public class Solution {
public int pathSum(TreeNode root, int sum) {
if(root == null) return 0;
return findPath(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
}
//return the number of path from root to cur that sum equal to target
private int findPath(TreeNode root, int sum){
if(root == null) return 0;
int res = 0;
if(root.val == sum){
res++;
}
res += findPath(root.left, sum - root.val);
res += findPath(root.right, sum - root.val);
return res;
}
}
Path Sum IV
Sum root to leaf numbers
做完Path Sum II之后发现这题跟那题思路一样
public class Solution {
public int sumNumbers(TreeNode root) {
if(root == null) return 0;
int[] res = new int[1];
int[] tmp = new int[1];
helper(res, tmp, root);
return res[0];
}
private void helper(int[] res, int[] tmp, TreeNode root){
if(root == null) return;
int tmpp = tmp[0];
tmp[0] = tmp[0] * 10 + root.val;
if(root.left == null && root.right == null){
res[0] += tmp[0];
}else{
helper(res, tmp, root.left);
helper(res, tmp, root.right);
}
tmp[0] = tmpp;
}
}
public int sumOfLeftLeaves(TreeNode root) {
//find left leaves
//then sum up
if(root == null) return 0;
int res = 0;
if(root.left != null){
if(root.left.left == null && root.left.right == null){
res += root.left.val;
}else{
res += sumOfLeftLeaves(root.left);
}
}
if(root.right != null){
res += sumOfLeftLeaves(root.right);
}
return res;
}
这题的Iterative的解法
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
Stack<TreeNode> stk = new Stack<>();
int res = 0;
stk.push(root);
while(!stk.isEmpty()){
TreeNode node = stk.pop();
if(node.left != null){
if(node.left.left == null && node.left.right == null){
res += node.left.val;
}else{
stk.push(node.left);
}
}
if(node.right != null){
stk.push(node.right);
}
}
return res;
}
这题属于没做过很难想的题目
因为treenode会包含负数
借着建立public int maxSumSinglePath(TreeNode root, int[] max)和计算single path max sum ending with curnode
来获得maximum path sum, max[0]
public class Solution {
public int maxPathSum(TreeNode root) {
if(root == null) return Integer.MIN_VALUE;
int[] max = new int[1];
max[0] = Integer.MIN_VALUE;
maxSumSinglePath(root, max);
return max[0];
}
//return the max sum of single path, including root
private int maxSumSinglePath(TreeNode root, int[] max){
if(root == null) return 0;
int left = Math.max(0, maxSumSinglePath(root.left, max));//since single path might be negative
int right = Math.max(0, maxSumSinglePath(root.right, max));
max[0] = Math.max(max[0], root.val + left + right);
return root.val + Math.max(left, right);
}
}
//2018/07/11 version
class Solution {
int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
maxSum(root);
return max;
}
// get maxSum value ending with cur node
private int maxSum(TreeNode root) {
if (root == null) return 0;
int left = Math.max(0, maxSum(root.left)); // 0 means not picking left subtree
int right = Math.max(0, maxSum(root.right));
// left and right must be non-negative value
max = Math.max(max, left + right + root.val);
return root.val + Math.max(left, right); // here can only pick one subtree path, not root.val + left + right
}
}
Longest Univalue Path
套用max path sum的思路
class Solution {
int max = 0;
public int longestUnivaluePath(TreeNode root) {
helper(root);
return max;
}
private int helper(TreeNode root) {
if (root == null) return 0;
int left = helper(root.left);
int right = helper(root.right);
if (root.left != null && root.left.val == root.val) {
left = left + 1;
} else {
left = 0;
}
if (root.right != null && root.right.val == root.val) {
right = right + 1;
} else {
right = 0;
}
max = Math.max(max, left + right);
return Math.max(left, right);
}
}
Second minimum node in a Binary Tree
class Solution {
public int findSecondMinimumValue(TreeNode root) {
if (root == null || root.left == null) return -1;
int left = root.left.val;
int right = root.right.val;
if (left == root.val) {
left = findSecondMinimumValue(root.left);
}
if (right == root.val) {
right = findSecondMinimumValue(root.right);
}
if (left != -1 && right != -1) {
return Math.min(left, right);
}
// left(right) == -1 means find the minimum Value
return Math.max(left, right);
}
}
class Solution {
Map<TreeNode, Integer> map = new HashMap<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
//use hashmap to store all the node from root path to target
//value will be the distance from target
findDistance(root, target);
List<Integer> res = new ArrayList<>();
dfs(root, target, K, map.get(root), res);
return res;
}
private int findDistance(TreeNode cur, TreeNode target) {
if (cur == null) return -1;
if (cur == target) {
map.put(cur, 0);
return 0;
}
int left = findDistance(cur.left, target);
if (left >= 0) {
map.put(cur, left + 1);
return left + 1;
}
int right = findDistance(cur.right, target);
if (right >= 0) {
map.put(cur, right + 1);
return right + 1;
}
return -1;
}
private void dfs(TreeNode root, TreeNode target, int K, int len, List<Integer> res) {
if (root == null) return;
if (map.containsKey(root)) len = map.get(root);
if (K == len) {
res.add(root.val);
//return;
}
dfs(root.left, target, K, len + 1, res);
dfs(root.right, target, K, len + 1, res);
}
}
(好题)
就是helper(TreeNode node, int val) 里把parent node.val传进来比较