Recurrence of the binary tree

1.Minimum Depth of Binary Tree

 // Recursive version, time complexity O( n), space complexity O(logn)
private static int minDepth(TreeNode root) {
if (root == null) return 0;
return Math.min(minDepth(root.left), minDepth(root.right))+1;
}

2.Maximum Depth of Binary Tree

 Same as above, recursive version, // Time complexity O(n), space complexity O(logn)
public int maxDepth(TreeNode root) {
    if (root == null) return 0;
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

3.Path Sum

return true, as there exist a root -to-leaf path 5->4->11->2 which sum is 22
// Time complexity O(n), space complexity O(logn)
public boolean hasPathSum(TreeNode root, int sum) {
    if (root == null) return false;
    if (root.left == null && root.right == null) // leaf
        return sum == root.val;
    return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
}

4.Path Sum II

 Same as above, return path // time Complexity O(n), space complexity O(logn)
public List> pathSum(TreeNode root, int sum) {
List> result = new ArrayList<>();
ArrayList cur = new ArrayList<>(); // intermediate result
pathSum(root, sum, cur, result);
return result;
}

private static void pathSum(TreeNode root, int gap, ArrayList cur, List> result) {
if (root == null) return;
cur.add(root.val);
if (root.left == null && root.right == null) {
if (gap == root.val) {result.add(new ArrayList<>(cur));}
}
pathSum(root.left, gap-root.val, cur, result);
pathSum(root.right, gap-root.val, cur, result);
cur.remove(cur.size()-1); //Added deleted
}

5.Binary Tree Maximum Path Sum

 // time complexity O(n), space complexity O(logn)
    private int max_sum;
private int dfs(TreeNode root) {
if (root == null) return 0;

int l = dfs(root.left);
int r = dfs(root.right);

int sum = root.val;
if (l> 0) sum += l;
if (r> 0) sum += r;

max_sum = Math.max(sum, max_sum);
return Math.max(l, r)> 0? Math.max(l, r) + root.val: root.val;
}

 // Recursive version, time complexity O(n), space complexity O(logn)
private static int minDepth(TreeNode root) {
if (root == null) return 0;
return Math.min(minDepth(root.left), minDepth(root.right))+1;
}

 Same as above, recursive version, // time complexity O(n), space complexity O(logn)
public int maxDepth(TreeNode root) {
    if (root == null) return 0;
    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22
// Time complexity O(n), space complexity O(logn)
public boolean hasPathSum(TreeNode root, int sum) {
    if (root == null) return false;
    if (root.left == null && root.right == null) // leaf
        return sum == root.val;
    return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);
}

 Same as above, return path // time complexity O(n), space complexity O(logn)
public List> pathSum(TreeNode root, int sum) {
List> result = new ArrayList<>();
ArrayList cur = new ArrayList<>(); // intermediate result
pathSum(root, sum, cur, result);
return result;
}

private static void pathSum(TreeNode root, int gap, ArrayList cur, List> result) {
if (root == null) return;
cur.add(root.val);
if (root.left == null && root.right == null) {
if (gap == root.val) {result.add(new ArrayList<>(cur));}
}
pathSum(root.left, gap-root.val, cur, result);
pathSum(root.right, gap-root.val, cur, result);
cur.remove(cur.size()-1); //Added deleted
}

 // time complexity O(n), space complexity O(logn)
    private int max_sum;
private int dfs(TreeNode root) {
if (root == null) return 0;

int l = dfs(root.left);
int r = dfs(root.right);

int sum = root.val;
if (l> 0) sum += l;
if (r> 0) sum += r;

max_sum = Math.max(sum, max_sum);
return Math.max(l, r)> 0? Math.max(l, r) + root.val: root.val;
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5265 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.