Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaansarii committed Aug 3, 2024
1 parent d706796 commit a3da6b5
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 36 deletions.
78 changes: 52 additions & 26 deletions Tree/Tree ds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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": [
Expand All @@ -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": [
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 "
]
}
Expand All @@ -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"
]
},
Expand Down
2 changes: 1 addition & 1 deletion Tree/easy/36_Minimum_Element_In_BST.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 32 additions & 3 deletions Tree/easy/58_Binary_Tree_Preorder_Traversal.py
Original file line number Diff line number Diff line change
@@ -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


40 changes: 37 additions & 3 deletions Tree/easy/59_Binary_Tree_Inorder_Traversal.py
Original file line number Diff line number Diff line change
@@ -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

42 changes: 39 additions & 3 deletions Tree/easy/60_Binary_Tree_Postorder_Traversal.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a3da6b5

Please sign in to comment.