Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}
65 changes: 65 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -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);
}
}