diff --git a/Tree/Tree ds.ipynb b/Tree/Tree ds.ipynb index 3b17553d..efb5bd40 100644 --- a/Tree/Tree ds.ipynb +++ b/Tree/Tree ds.ipynb @@ -494,7 +494,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -520,20 +520,33 @@ " if root:\n", " self.postOrder(root.left) # Recur on the left child\n", " self.postOrder(root.right) # Recur on the right child\n", - " print(root.data, end=\" \") # Print the data of the root\n" + " print(root.data, end=\" \") # Print the data of the root\n", + "\n", + " def maxDepth(self, root) -> int:\n", + " \n", + " if not root:\n", + " return 0\n", + " left_depth = self.maxDepth(root.left)\n", + " right_depth = self.maxDepth(root.right)\n", + " return right_depth\n", + "\n", + " # return max(left_depth,right_depth) +1" ] }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 13, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "17 15 21 17 17 19 23 None\n" - ] + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -544,12 +557,14 @@ "root.left.right=Node(15)\n", "root.right.left=Node(17)\n", "root.right.right=Node(17)\n", - "print(root.postOrder(root))" + "root.right.right.left=Node(17)\n", + "# print(root.postOrder(root))\n", + "root.maxDepth(root)" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -575,18 +590,18 @@ " \n", " def inOrder(self,root):\n", " stack=[]\n", - " if root:\n", - " stack.append(root)\n", - " while len(stack)>0:\n", - " if root.left:\n", - " popped=stack.pop()\n", - " print(popped.data, end=' ')\n", - " if root:\n", - " stack.append(root)\n", - " if popped.right:\n", - " stack.append(popped.right)\n", - "\n", - " else: return -1\n", + " currunt=root\n", + " while stack or currunt:\n", + " if currunt:\n", + " stack.append(currunt)\n", + " currunt=currunt.left\n", + " elif stack:\n", + " currunt=stack.pop()\n", + " print(currunt.data, end=' ')\n", + " currunt=currunt.right\n", + "\n", + " \n", + " \n", "\n", " def postOrder(self,root):\n", " stack1=[]\n", @@ -595,11 +610,14 @@ " stack1.append(current)\n", " while stack1:\n", " current=stack1.pop()\n", - " stack2.append(current.data)\n", + " stack2.append(current.data) \n", + "\n", " if current.left:\n", " stack1.append(current.left)\n", + "\n", " if current.right:\n", " stack1.append(current.right)\n", + " \n", " while stack2:\n", " data=stack2.pop()\n", " print(data, end=' ')\n", @@ -608,13 +626,18 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "this is preOrder traversal: \n", + "1 2 5 4 3 7 6 \n", + "this is inOrder traversal: \n", + "5 2 4 1 7 3 6 \n", + "this is postOrder traversal: \n", "5 4 2 7 6 3 1 " ] } @@ -627,8 +650,11 @@ "root.left.left=Node(5)\n", "root.right.right=Node(6)\n", "root.right.left=Node(7)\n", - "# root.preOrder(root)\n", - "# root.inOrder(root)\n", + "print(\"this is preOrder traversal: \")\n", + "root.preOrder(root)\n", + "print(\"\\nthis is inOrder traversal: \")\n", + "root.inOrder(root)\n", + "print(\"\\nthis is postOrder traversal: \")\n", "root.postOrder(root)\n" ] }, diff --git a/Tree/easy/36_Minimum_Element_In_BST.py b/Tree/easy/36_Minimum_Element_In_BST.py index 383ade84..3fde8452 100644 --- a/Tree/easy/36_Minimum_Element_In_BST.py +++ b/Tree/easy/36_Minimum_Element_In_BST.py @@ -2,5 +2,5 @@ # URL: https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1 def solution(): - # TODO: Implement the solution for Minimum_Element_In_BST + # TODO: pass diff --git a/Tree/easy/58_Binary_Tree_Preorder_Traversal.py b/Tree/easy/58_Binary_Tree_Preorder_Traversal.py index ba8d25e2..42537019 100644 --- a/Tree/easy/58_Binary_Tree_Preorder_Traversal.py +++ b/Tree/easy/58_Binary_Tree_Preorder_Traversal.py @@ -1,6 +1,35 @@ # Problem: Binary_Tree_Preorder_Traversal # URL: https://leetcode.com/problems/binary-tree-preorder-traversal/description/ -def solution(): - # TODO: Implement the solution for Binary_Tree_Preorder_Traversal - pass +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + # ele=[] + # if root: + # stack=[] + # stack.append(root) + # while len(stack)>0: + # popped=stack.pop() + # ele.append(popped.val) + # if popped.right: + # stack.append(popped.right) + # if popped.left: + # stack.append(popped.left) + # return ele + + ## Recurssive approach + def preOrder(node,ele): + if node: + ele.append(node.val) + preOrder(node.left,ele) + preOrder(node.right,ele) + ele=[] + preOrder(root,ele) + return ele + + diff --git a/Tree/easy/59_Binary_Tree_Inorder_Traversal.py b/Tree/easy/59_Binary_Tree_Inorder_Traversal.py index e3d18723..c04ea71d 100644 --- a/Tree/easy/59_Binary_Tree_Inorder_Traversal.py +++ b/Tree/easy/59_Binary_Tree_Inorder_Traversal.py @@ -1,6 +1,40 @@ # Problem: Binary_Tree_Inorder_Traversal # URL: https://leetcode.com/problems/binary-tree-inorder-traversal/description/ -def solution(): - # TODO: Implement the solution for Binary_Tree_Inorder_Traversal - pass +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + # if not root: + # return + # def inOrder(root,ele): + # if root.left: + # inOrder(root.left,ele) + # ele.append(root.val) + # if root.right: + # inOrder(root.right,ele) + + # ele=[] + # inOrder(root,ele) + # return ele; + + ## iterative approach + if not root: + return + traversed=[] + stack=[] + curr=root + while stack or curr: + if curr: + stack.append(curr) + curr=curr.left + elif len(stack)>0: + curr=stack.pop() + traversed.append(curr.val) + curr=curr.right + return traversed + diff --git a/Tree/easy/60_Binary_Tree_Postorder_Traversal.py b/Tree/easy/60_Binary_Tree_Postorder_Traversal.py index 623cb3ec..effbf2bd 100644 --- a/Tree/easy/60_Binary_Tree_Postorder_Traversal.py +++ b/Tree/easy/60_Binary_Tree_Postorder_Traversal.py @@ -1,6 +1,42 @@ # Problem: Binary_Tree_Postorder_Traversal # URL: https://leetcode.com/problems/binary-tree-postorder-traversal/description/ -def solution(): - # TODO: Implement the solution for Binary_Tree_Postorder_Traversal - pass +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: + # if not root: + # return + # def postOrder(root,ele): + # if root.left: + # postOrder(root.left,ele) + # if root.right: + # postOrder(root.right,ele) + # ele.append(root.val) + # ele=[] + # postOrder(root,ele) + # return ele + + ## iterative approach + if not root: + return + stack1=[] + stack2=[] + current=root + stack1.append(current) + while stack1: + current=stack1.pop() + stack2.append(current.val) + if current.left: + stack1.append(current.left) + if current.right: + stack1.append(current.right) + traversed=[] + while stack2: + data=stack2.pop() + traversed.append(data) + return traversed