Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#106. Construct Binary Tree from Inorder and Postorder Traversal
# Time Complexity :O(n)
# Space Complexity :O(h)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :no


# Your code here along with comments explaining your approach
#from postorder identify the root element from last element in aray
# fint the root element index in the inorder array
# Inorder array, everything on left of root is left subtree of root and everything on right is right subtree of root
#make recusive calls to implement the above logic
#time - O(n)
#space - O(h)

# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
self.idx=len(postorder)-1 # index of postorder list
d=dict()##map for maintaining index of values in inorder
for i in range(0, len(inorder)):
d[inorder[i]]=i
return self.helper(postorder, d, 0,len(inorder)-1)

def helper(self,postorder, d, start, end):
if(start>end):
return None

curr=postorder[self.idx]
print(self.idx)
root=TreeNode(curr)
self.idx-=1
root.right=self.helper(postorder, d, d[curr]+1, end)#right subtree of curr node
root.left=self.helper(postorder, d, start, d[curr]-1)#left subtree of curr node
return root
30 changes: 30 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#129. Sum Root to Leaf Numbers
# Time Complexity :O(n)
# Space Complexity :O(h)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :no


# Your code here along with comments explaining your approach
#resursively call the nodes in tree either is preorder,inorder,postorder. Maintin the sum at each node by passing sum as paramerter in recursion
# 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 sumNumbers(self, root: Optional[TreeNode]) -> int:
self.res=0 # results as global variable
self.helper(root,0)#call helper passing initial sum as 0
return self.res

def helper(self, root, sum):
#condition
if root is None:
return
sum=sum*10+root.val
if root.left is None and root.right is None: # if leaf node then add the sum to result
self.res+=sum
self.helper(root.left, sum)
self.helper(root.right, sum)