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
36 changes: 36 additions & 0 deletions Problem45.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Time Complexity : O(N)
// Space Complexity : O(N)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this : No

class Solution {
private Map<Integer, Integer> map;
private int postOrderIdx;
public TreeNode buildTree(int[] inorder, int[] postorder) {
this.map = new HashMap<>();
this.postOrderIdx = postorder.length-1;

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
if (start > end) return null;

//logic
int rootVal = postorder[this.postOrderIdx];
int rootIdx = map.get(rootVal);
this.postOrderIdx--;

TreeNode root = new TreeNode(rootVal);

root.right = helper(postorder, rootIdx + 1, end);
root.left = helper(postorder, start, rootIdx - 1);

return root;
}
}
25 changes: 25 additions & 0 deletions Problem46.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time Complexity : O(N)
// Space Complexity : O(h)

class Solution {
private int result;
public int sumNumbers(TreeNode root) {
this.result = 0;
helper(root, 0);
return result;
}

private void helper(TreeNode root, int currSum) {
//base
if (root == null) return;

//logic
currSum = currSum * 10 + root.val;

helper(root.left, currSum);
if (root.left == null && root.right == null) {
result += currSum;
}
helper(root.right, currSum);
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.