From 330f5b90dab26da3b8f541065947081cb939b153 Mon Sep 17 00:00:00 2001 From: YogeshPardeshi <31638743+YogeshPardeshi@users.noreply.github.com> Date: Sat, 23 May 2026 11:43:32 -0400 Subject: [PATCH 1/2] Create Problem1.java Problem1 --- Problem1.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Problem1.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..6b66bdc2 --- /dev/null +++ b/Problem1.java @@ -0,0 +1,45 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Problem1 { + HashMap map; + int postOrderIdx; + public TreeNode buildTree(int[] inorder, int[] postorder) { + + this.map = new HashMap<>(); + postOrderIdx = postorder.length-1; + + for(int i=0; i end) return null; + + int rootVal = postorder[postOrderIdx]; + int rootIdx = map.get(rootVal); + postOrderIdx--; + + TreeNode node = new TreeNode(rootVal); + + node.right = helper(postorder,rootIdx+1,end); + node.left = helper(postorder,start,rootIdx-1); + + return node; + } +} From 3b1f423b60104f5a2417a304fca786355d234491 Mon Sep 17 00:00:00 2001 From: YogeshPardeshi <31638743+YogeshPardeshi@users.noreply.github.com> Date: Sat, 23 May 2026 11:45:20 -0400 Subject: [PATCH 2/2] Create Problem2.java Problem2 --- Problem2.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Problem2.java diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..31189424 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Problem2 { + int result; + public int sumNumbers(TreeNode root) { + this.result = 0; + helper(root, 0); + return result; + } + + private void helper(TreeNode root, int currNum){ + // base case + if(root == null) return; + + currNum = currNum * 10 + root.val; + + helper(root.right, currNum); + + if(root.left == null && root.right == null){ + result += currNum; + } + + helper(root.left, currNum); + } +}