From 3f57c8a63a3c75ab880331ac1149b282c780c802 Mon Sep 17 00:00:00 2001 From: tmujje Date: Fri, 14 May 2021 01:07:27 -0400 Subject: [PATCH 1/2] Trees2 - Submission --- InOrderPostOrderTraversal.java | 31 +++++++++++++++++++++++++++++++ SumRootToLeafNumbers.java | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 InOrderPostOrderTraversal.java create mode 100644 SumRootToLeafNumbers.java diff --git a/InOrderPostOrderTraversal.java b/InOrderPostOrderTraversal.java new file mode 100644 index 00000000..e0f830b7 --- /dev/null +++ b/InOrderPostOrderTraversal.java @@ -0,0 +1,31 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n power 2) we are iterating through the inorder array every time through our recursive calls. +//Space Complexity: O(1) since we are not taking any extra space +class Solution { + public TreeNode buildTree(int[] inorder, int[] postorder) { + + if(postorder.length == 0) return null; + int idx = -1; + int rootVal = postorder[postorder.length - 1]; + TreeNode root = new TreeNode(rootVal); + + //Find the index at which the root value exists in the inorder + for(int i = 0; i < inorder.length; i++){ + if(inorder[i] == rootVal){ + idx = i; + break; + } + } + + int[] inorderLeft = Arrays.copyOfRange(inorder, 0, idx); + int[] inorderRight = Arrays.copyOfRange(inorder, idx+1, inorder.length); + int[] postorderLeft = Arrays.copyOfRange(postorder, 0, idx); + int[] postorderRight = Arrays.copyOfRange(postorder, idx, postorder.length - 1); + + root.left = buildTree(inorderLeft, postorderLeft); + root.right = buildTree(inorderRight, postorderRight); + return root; + } +} diff --git a/SumRootToLeafNumbers.java b/SumRootToLeafNumbers.java new file mode 100644 index 00000000..585ac01b --- /dev/null +++ b/SumRootToLeafNumbers.java @@ -0,0 +1,25 @@ +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + +//Time Complexity : O(n) since we are traversing across all the nodes in the tree in worst case scenario +//Space Complexity: O(1) since we are not taking any extra space + +class Solution { + public int result; + public int sumNumbers(TreeNode root) { + helper(root, 0); + return result; + } + + private void helper(TreeNode root, int rSum){ + //Base Condition + if(root == null) return; + + rSum = rSum*10 + root.val; + helper(root.left, rSum); + if(root.left == null && root.right == null){ //If we reached leaf node, then add to the result + result += rSum; + } + helper(root.right, rSum); + } +} \ No newline at end of file From 81a4551666ec83a119168d8994188be73547ecdb Mon Sep 17 00:00:00 2001 From: TejBharath Mujje Date: Wed, 26 Aug 2026 22:26:07 -0400 Subject: [PATCH 2/2] Complete Trees-2 Assignment --- InOrderPostOrderBinaryTree.java | 47 +++++++++++++++++++++++++++++++++ InOrderPostOrderTraversal.java | 31 ---------------------- SumRootToLeaf | 46 ++++++++++++++++++++++++++++++++ SumRootToLeafNumbers.java | 25 ------------------ 4 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 InOrderPostOrderBinaryTree.java delete mode 100644 InOrderPostOrderTraversal.java create mode 100644 SumRootToLeaf delete mode 100644 SumRootToLeafNumbers.java diff --git a/InOrderPostOrderBinaryTree.java b/InOrderPostOrderBinaryTree.java new file mode 100644 index 00000000..a9f163b5 --- /dev/null +++ b/InOrderPostOrderBinaryTree.java @@ -0,0 +1,47 @@ +/** + * 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; + * } + * } + */ +//Time Complexity - O(n2) +//Space Complexity - O(n2) +class InOrderPostOrderBinaryTree { + public TreeNode buildTree(int[] preorder, int[] inorder) { + + if (preorder.length == 0) return null; + + int rootVal = preorder[0]; + int rootIdx = -1; + + for(int i=0; i