LeetCode-二叉树总结(一)

二叉树

一棵二叉树要么是空树,要么有指向子结点的指针,每个指针指向一棵树。树是一种递归结构,很多树的问题可以使用递归来处理。

树的高度

104. Maximum Depth of Binary Tree (Easy)

class Solution {
    public int maxDepth(TreeNode root) {
        if (null == root) {
            return 0;
        }
        if (null == root.left && null == root.right) {
            return 1;
        }
        int leftHeight = 0;
        int rightHeight = 0;
        if (root.left != null) {
            leftHeight = maxDepth(root.left) + 1;
        }
        if (root.right != null) {
            rightHeight = maxDepth(root.right) + 1;
        }
        return leftHeight > rightHeight ? leftHeight : rightHeight; 
    }
}

平衡树

110. Balanced Binary Tree (Easy)

    3
   / \
  9  20
    /  \
   15   7

平衡树左右子树高度差都小于等于 1

class Solution {
    boolean flag = true;
    public boolean isBalanced(TreeNode root) {
        if (null == root) {
            return true;
        }
        maxDepth(root);
        return flag;
    }
    public int maxDepth(TreeNode root) {
        if (null == root) {
            return 0;
        }
        if (null == root.left && null == root.right) {
            return 1;
        }
        int left = 0;
        int right = 0;
        left = maxDepth(root.left) + 1;
        right = maxDepth(root.right) + 1;
        if (Math.abs(left - right) > 1) {
            flag = false;
        }
        return left > right ? left : right;
    }
}

两节点的最长路径

543. Diameter of Binary Tree (Easy)

Input:

         1
        / \
       2  3
      / \
     4   5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
class Solution {
    int max = Integer.MIN_VALUE;
    public int diameterOfBinaryTree(TreeNode root) {
        if (null == root) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        max = Math.max(max, left + right);
        diameterOfBinaryTree(root.left);
        diameterOfBinaryTree(root.right);
        return max;
    }
    public int maxDepth(TreeNode root) {
        if (null == root) {
            return 0;
        }
        if (null == root.left && null == root.right) {
            return 1;
        }
        int left = maxDepth(root.left) + 1;
        int right = maxDepth(root.right) + 1;
        return left > right ? left : right;
    }
}

翻转树

226. Invert Binary Tree (Easy)

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (null == root) {
            return null;
        }
        TreeNode left = root.left;
        root.left = invertTree(root.right);
        root.right = invertTree(left);
        return root;
    }
}

归并两棵树

617. Merge Two Binary Trees (Easy)

Input:
       Tree 1                     Tree 2
          1                         2
         / \                       / \
        3   2                     1   3
       /                           \   \
      5                             4   7

Output:
         3
        / \
       4   5
      / \   \
     5   4   7
class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (null == t1 && null == t2) {
            return null;
        }
        if (null == t1) {
            return t2;
        }
        if (null == t2) {
            return t1;
        }
        t1.val += t2.val;
        t1.left = mergeTrees(t1.left, t2.left);
        t1.right = mergeTrees(t1.right, t2.right);
        return t1;
    }
}

判断路径和是否等于一个数

Leetcdoe : 112. Path Sum (Easy)

Given the below binary tree and sum = 22,

              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

路径和定义为从 root 到 leaf 的所有节点的和。

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (null == root) {
            return false;
        }
        if (sum == root.val && null == root.left && null == root.right) {
            return true;
        }
        boolean left = false;
        boolean right = false;
        if (root.left != null) {
            left = hasPathSum(root.left, sum - root.val);
        }
        if (root.right != null) {
            right = hasPathSum(root.right, sum - root.val);
        }
        return left | right;
    }
}

统计路径和等于一个数的路径数量

437. Path Sum III (Easy)

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11

路径不一定以 root 开头,也不一定以 leaf 结尾,但是必须连续。

class Solution {
    public int pathSum(TreeNode root, int sum) {
        if (null == root) {
            return 0;
        }
        int num = help(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
        return num;
    }
    public int help(TreeNode root, int sum) {
        if (null == root) {
            return 0;
        }
        int ret = 0;
        if (sum == root.val) {
            ret++;
        }
        ret += help(root.left, sum - root.val) + help(root.right, sum - root.val);
        return ret;
    }
}

子树

572. Subtree of Another Tree (Easy)

Given tree s:
     3
    / \
   4   5
  / \
 1   2

Given tree t:
   4
  / \
 1   2

Return true, because t has the same structure and node values with a subtree of s.

Given tree s:

     3
    / \
   4   5
  / \
 1   2
    /
   0

Given tree t:
   4
  / \
 1   2

Return false.
class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if (null == s && null == t) {
            return true;
        }
        if (null == s || null == t) {
            return false;
        }
        boolean left = false;
        boolean right = false;
        if (null == s.left && null == s.right) {
            return help(s, t);
        }
        if (s.left != null) {
            left = help(s, t) || isSubtree(s.left, t);
        }
        if (s.right != null) {
            right = help(s, t) || isSubtree(s.right, t);
        }
        return left || right;
    }
    public boolean help(TreeNode s, TreeNode t) {
        if (null == s && null == t) {
            return true;
        }
        if (null == s || null == t) {
            return false;
        }
        if (s.val != t.val) {
            return false;
        }
        return help(s.left, t.left) && help(s.right, t.right);
    }
}

树的对称

101. Symmetric Tree (Easy)

    1
   / \
  2   2
 / \ / \
3  4 4  3
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (null == root) {
            return true;
        }
        if (null == root.left && null == root.right) {
            return true;
        }
        if (null == root.left || null == root.right) {
            return false;
        }
        if (root.left.val != root.right.val) {
            return false;
        }
        return help(root.left.left, root.right.right) && help(root.left.right, root.right.left);
    }
    public boolean help(TreeNode t1, TreeNode t2) {
        if (null == t1 && null == t2) {
            return true;
        }
        if (null == t1 || null == t2) {
            return false;
        }
        if (t1.val != t2.val) {
            return false;
        }
        return help(t1.left, t2.right) && help(t1.right, t2.left);
    }
}

最小路径

111. Minimum Depth of Binary Tree (Easy)

树的根节点到叶子节点的最小路径长度

class Solution {
    public int minDepth(TreeNode root) {
        if (null == root) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int level = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                if (null == node.left && null == node.right) {
                    return level;
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }
            level++;
        }
        return level;
    }
}

统计左叶子节点的和

404. Sum of Left Leaves (Easy)

    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
class Solution {
    int sum = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left != null) {
            if (null == root.left.left && null == root.left.right) {
                sum += root.left.val;
            }
            sumOfLeftLeaves(root.left);
        }
        if (root.right != null) {
            sumOfLeftLeaves(root.right);
        }
        return sum;
    }
}

相同节点值的最大路径长度

687. Longest Univalue Path (Easy)

             1
            / \
           4   5
          / \   \
         4   4   5

Output : 2
class Solution {
    int max = 0;
    public int longestUnivaluePath(TreeNode root) {
        if (null == root) {
            return 0;
        }
        dfs(root);
        return max;
    }
    public int dfs(TreeNode root) {
        if (null == root) {
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        if (root.left != null && root.left.val == root.val) {
            left++;
        } else {
            left = 0;
        }
        if (root.right != null && root.right.val == root.val) {
            right++;
        } else {
            right = 0;
        }
        max = Math.max(max, left + right);
        return Math.max(left, right);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值