Skip to content

Commit

Permalink
Day 29 - Recursion III
Browse files Browse the repository at this point in the history
  • Loading branch information
xckomorebi committed Sep 14, 2022
1 parent 0cc6356 commit 884fd00
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions RecursionIII.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.*;
import utils.*;

public class RecursionIII {
Expand Down Expand Up @@ -48,4 +49,170 @@ public int maxPathSumLeafToRoot(TreeNode root) {
}
return Math.max(left, right) + root.key;
}

/**
* Binary Tree Path Sum To Target III
* <p>
* Given a binary tree in which each node contains an integer number. Determine
* if there exists a path (the path can only be from one node to itself or to
* any of its descendants), the sum of the numbers on the path is the given
* target number.
*/
public boolean exist(TreeNode root, int target) {
this.map = new HashMap<>();
this.found = false;
map.put(0, 1);
exist(root, 0, target);
return this.found;
}

private boolean found;
private Map<Integer, Integer> map;

private void exist(TreeNode node, int sum, int target) {
if (found) {
return;
}
if (node == null) {
return;
}
int curSum = sum + node.key;
if (map.containsKey(curSum - target)) {
found = true;
return;
}

map.put(curSum, map.containsKey(curSum) ? map.get(curSum) + 1 : 1);
exist(node.left, curSum, target);
exist(node.right, curSum, target);

if (map.get(curSum) == 1) {
map.remove(curSum);
} else {
map.put(curSum, map.get(curSum) - 1);
}
}

/**
* Maximum Path Sum Binary Tree III
* <p>
* Given a binary tree in which each node contains an integer number. Find the
* maximum possible subpath sum(both the starting and ending node of the subpath
* should be on the same path from root to one of the leaf nodes, and the
* subpath is allowed to contain only one node).
*/
public int maxPathSum2(TreeNode root) {
this.max = Integer.MIN_VALUE;
maxPathSum(root, 0);
return this.max;
}

private void maxPathSum(TreeNode node, int sum) {
if (node == null) {
return;
}

int curSum = sum + node.key;
this.max = Math.max(this.max, curSum);
curSum = Math.max(0, curSum);
maxPathSum(node.left, curSum);
maxPathSum(node.right, curSum);
}

/**
* Flatten Binary Tree to Linked List
* <p>
* Given a binary tree, flatten it to a linked list in-place.
*/
public TreeNode flatten(TreeNode root) {
if (root == null) {
return root;
}
flattenHelper(root);
return root;
}

public TreeNode flattenHelper(TreeNode node) {
if (node.left == null && node.right == null) {
return node;
}

if (node.left == null) {
return flattenHelper(node.right);
}
if (node.right == null) {
node.right = node.left;
node.left = null;
return flattenHelper(node.right);
}

TreeNode left = flattenHelper(node.left);
TreeNode right = flattenHelper(node.right);
left.right = node.right;
node.right = node.left;
node.left = null;

return right;
}

public TreeNode reconstruct(int[] in, int[] pre) {
this.map = new HashMap<>();
for (int i = 0; i < in.length; i++) {
map.put(in[i], i);
}
TreeNode root = reconstruct(in, 0, in.length - 1, pre, 0, pre.length - 1);
return root;
}

private TreeNode reconstruct(int[] in, int inLeft, int inRight, int[] pre, int preLeft, int preRight) {
if (inLeft > inRight) {
return null;
}
TreeNode node = new TreeNode(pre[preLeft]);
int mid = map.get(pre[preLeft]);
node.left = reconstruct(in, inLeft, mid - 1, pre, preLeft + 1, preLeft + mid - inLeft);
node.right = reconstruct(in, mid + 1, inRight, pre, preRight + mid - inRight + 1, preRight);
return node;
}

public TreeNode reconstruct(int[] post) {
TreeNode root = reconstruct(post, 0, post.length - 1);
return root;
}

private TreeNode reconstruct(int[] post, int left, int right) {
if (right < left) {
return null;
}

TreeNode node = new TreeNode(post[right]);
if (left == right) {
return node;
}
int mid = binarySearch(post, left, right - 1, post[right]);
node.left = reconstruct(post, left, mid);
node.right = reconstruct(post, mid + 1, right - 1);
return node;

}

private int binarySearch(int[] post, int left, int right, int target) {
if (post[left] > target) {
return left - 1;
}
if (post[right] < target) {
return right;
}

while (left < right - 1) {
int mid = left + (right - left) / 2;
if (post[mid] < target) {
left = mid;
} else {
right = mid;
}
}

return left;
}
}

0 comments on commit 884fd00

Please sign in to comment.