From c56112a02834127979874edecbeb9a1660b7fe0c Mon Sep 17 00:00:00 2001 From: yashhh-23 Date: Tue, 7 Jul 2026 21:56:49 +0530 Subject: [PATCH] completed trees-2 --- TreeNode.java | 51 ++++++++++++++++++++++++++++++++++++++++++++++ sumroottoleaf.java | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 TreeNode.java create mode 100644 sumroottoleaf.java diff --git a/TreeNode.java b/TreeNode.java new file mode 100644 index 00000000..7be1c8b6 --- /dev/null +++ b/TreeNode.java @@ -0,0 +1,51 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) - for the hashmap and recursion stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : thinking about the order of traversal and how to use the postorder index correctly + +// Your code here along with comments explaining your approach: using hashmap to store the inorder values and their indices, then using postorder index to get the root value and recursively building the left and right subtrees. The postorder index is decremented after each root is processed, and the right subtree is built before the left subtree since we are processing postorder from the end. +/** + * 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 Solution { + 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; + } +} diff --git a/sumroottoleaf.java b/sumroottoleaf.java new file mode 100644 index 00000000..a22c02f5 --- /dev/null +++ b/sumroottoleaf.java @@ -0,0 +1,45 @@ +// Time Complexity : O(n) +// Space Complexity : O(n) - for the recursion stack +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : yes while calculating the current number, I was not multiplying the previous number by 10 before adding the current node value, which was giving me wrong results. After fixing that, it worked fine. + + +// Your code here along with comments explaining your approach: using helper function to traverse the tree in a depth-first manner, keeping track of the current number formed by the path from root to leaf. When a leaf node is reached, add the current number to the result. The current number is calculated by multiplying the previous number by 10 and adding the current node's value. +/** + * 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 Solution { + 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; + + if(root.left == null && root.right == null){ + result += currNum; + } + + helper(root.left, currNum); + + helper(root.right, currNum); + } +}