From 85199cf6b02011faea3ebc05ec200c2992cd39ee Mon Sep 17 00:00:00 2001 From: sainathek Date: Thu, 19 Mar 2026 20:14:02 -0400 Subject: [PATCH] Done trees-2 --- problem1.java | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ problem2.java | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 problem1.java create mode 100644 problem2.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..4532a76e --- /dev/null +++ b/problem1.java @@ -0,0 +1,76 @@ + + /** + Time Complexity : O(N) + Explanation: + - We traverse postorder once. + - Each node lookup in inorder is O(1) using HashMap. + So overall linear time. + + Space Complexity : O(N) + Explanation: + - HashMap stores inorder indices. + - Recursion stack can go up to N in worst case (skewed tree). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about how to split left and right subtrees. + Later understood: + - postorder gives root first + - Inorder gives left | root | right + Used a global index for preorder and a HashMap to quickly + find root position in inorder to divide subtrees correctly. + */ +/** + * 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 idx; + HashMap map; + + public TreeNode buildTree(int[] inorder,int[] postorder) { + + idx = postorder.length-1 ; + map = new HashMap<>(); + + // Store inorder value -> index mapping + for (int i = 0; i < inorder.length; i++) { + map.put(inorder[i], i); + } + + return helper(postorder, 0, inorder.length - 1); + } + + private TreeNode helper(int[] postorder, int start, int end) { + + // Base case + if (start > end) return null; + + // Root from preorder + int rootVal = postorder[idx]; + TreeNode root = new TreeNode(rootVal); + + int rootIdx = map.get(rootVal); + idx--; + + // Build left and right subtree + root.right = helper(postorder, rootIdx + 1, end); + root.left = helper(postorder, start, rootIdx - 1); + + return root; + } +} \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..bb8825ba --- /dev/null +++ b/problem2.java @@ -0,0 +1,65 @@ + /** + Time Complexity : O(N) + Explanation: + We traverse each node once using DFS. + + Space Complexity : O(H) + Explanation: + Recursion stack depends on height of the tree. + Worst case (skewed tree) → O(N), balanced → O(log N). + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially confused about how to carry the number formed + from root to current node. + Fixed it by passing the accumulated sum in recursion: + sum = sum * 10 + current node value + and adding it to result when a leaf node is reached. + */ + +/** + * 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 sum) { + + if (root == null) return; + + // Build number from root to current node + sum = sum * 10 + root.val; + + // If leaf node, add to result + if (root.left == null && root.right == null) { + result += sum; + return; + } + + helper(root.left, sum); + helper(root.right, sum); + } +} \ No newline at end of file