From b1ce61290592624990d0503b8cfbca8d586b7fbb Mon Sep 17 00:00:00 2001 From: Shakthi Nandana Date: Wed, 29 Apr 2026 20:30:15 -0400 Subject: [PATCH 1/3] slolution to prob2 --- problem2.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 problem2.py diff --git a/problem2.py b/problem2.py new file mode 100644 index 00000000..dc72a7d8 --- /dev/null +++ b/problem2.py @@ -0,0 +1,30 @@ +# // Time Complexity : O(n) +# // Space Complexity : O(h) where h is the height of the tree, for recursive stack space +# // Did this code successfully run on Leetcode : Yes + +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + def sumNumbers(self, root): + """ + :type root: Optional[TreeNode] + :rtype: int + """ + self.res=0 + + def helper(root,cur): + if root == None: return + cur=cur*10 + root.val + if root.left==None and root.right==None: + self.res+=cur + return + helper(root.left,cur) + helper(root.right,cur) + + helper(root,0) + + return self.res From 4f7253450ac91cc6975d161e0ce90126b60aa7af Mon Sep 17 00:00:00 2001 From: Shakthi Nandana Date: Wed, 29 Apr 2026 23:10:01 -0400 Subject: [PATCH 2/3] solutions --- build_with_postorder_inorder.py | 37 ++++++++++++++++++++++++++++++ problem1.py | 0 problem2.py => sum_root_to_leaf.py | 0 3 files changed, 37 insertions(+) create mode 100644 build_with_postorder_inorder.py create mode 100644 problem1.py rename problem2.py => sum_root_to_leaf.py (100%) diff --git a/build_with_postorder_inorder.py b/build_with_postorder_inorder.py new file mode 100644 index 00000000..e74ae560 --- /dev/null +++ b/build_with_postorder_inorder.py @@ -0,0 +1,37 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) +# Did this code successfully run on Leetcode : Yes + +# Definition for a binary tree node. +class TreeNode(object): + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right +class Solution(object): + def buildTree(self, inorder, postorder): + """ + :type inorder: List[int] + :type postorder: List[int] + :rtype: Optional[TreeNode] + """ + hmap={} + self.rootindex=len(postorder)-1 + for i in range(len(inorder)): + hmap[inorder[i]]=i + + def helper(in_st,in_end): + if (in_st>in_end): + return None + + rootval=postorder[self.rootindex] + in_root_idx=hmap[rootval] + root = TreeNode(rootval) + self.rootindex-=1 + root.right = helper(in_root_idx+1,in_end) + root.left = helper(in_st,in_root_idx-1) + + return root + + + return helper(0,len(postorder)-1) \ No newline at end of file diff --git a/problem1.py b/problem1.py new file mode 100644 index 00000000..e69de29b diff --git a/problem2.py b/sum_root_to_leaf.py similarity index 100% rename from problem2.py rename to sum_root_to_leaf.py From 9392c48b8a10436363f3d451654021def56e22de Mon Sep 17 00:00:00 2001 From: Shakthi Nandana Date: Wed, 29 Apr 2026 23:13:07 -0400 Subject: [PATCH 3/3] update --- problem1.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 problem1.py diff --git a/problem1.py b/problem1.py deleted file mode 100644 index e69de29b..00000000