美文网首页
LeetCode | 树相关题目

LeetCode | 树相关题目

作者: 念人远乡 | 来源:发表于2019-02-15 00:01 被阅读37次
    • LeetCode 104.二叉树的最大深度

    给定一个二叉树,找出其最大深度。
    二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
    说明: 叶子节点是指没有子节点的节点。
    示例:
    给定二叉树 [3,9,20,null,null,15,7],
    返回它的最大深度 3 。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        private int max_depth;
        public int maxDepth(TreeNode root) {
            if(root == null){
                return 0;
            }
            int left_depth = maxDepth(root.left);
            int right_depth = maxDepth(root.right);
            max_depth = Math.max(left_depth,right_depth)+1;
            return max_depth;
        }
    }
    
    • LeetCode 114.二叉树的最小深度

    给定一个二叉树,找出其最小深度。
    最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
    说明:叶子节点是指没有子节点的节点。
    示例:
    给定二叉树 [3,9,20,null,null,15,7],
    返回它的最小深度 2.

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int minDepth(TreeNode root) {
            if (root == null){
                return 0;
            }
            int left = minDepth(root.left);
            int right = minDepth(root.right);
            if (left == 0 || right == 0){ //某一边为空子树
                return left + right + 1;
        }
            return Math.min(left, right) + 1;
        }
    }
    
    • LeetCode 226.翻转二叉树

    翻转一棵二叉树。
    示例:
    输入:[4,2,7,1,3,6,9]
    输出:[4,7,2,9,6,3,1]

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode invertTree(TreeNode root) {
             if (root == null){
                 return null;
             }
            TreeNode left_temp = root.left; //暂时保存左子树的内容
            root.left = invertTree(root.right);
            root.right = invertTree(left_temp);
            return root;
        }
    }
    
    • LeetCode 226.翻转二叉树

    给定一个二叉树,检查它是否是镜像对称的。
    例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
    但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSymmetric(TreeNode root) {
            return isMirror(root,root);
        }
        public boolean isMirror(TreeNode t1,TreeNode t2){
            if(t1 == null && t2 == null){
                return true;
            }
            if(t1 == null || t2 == null){
                return false;
            }
            return(t1.val == t2.val) && isMirror(t1.right,t2.left) && isMirror(t1.left,t2.right);
        }
    }
    
    • LeetCode 110.平衡二叉树

    给定一个二叉树,判断它是否是高度平衡的二叉树。
    本题中,一棵高度平衡二叉树定义为:
    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
    示例 1:
    给定二叉树 [3,9,20,null,null,15,7]
    返回 true 。
    示例 2:
    给定二叉树 [1,2,2,3,3,null,null,4,4]
    返回 false 。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        private boolean result = true;
        public boolean isBalanced(TreeNode root) {
            maxDepth(root);
            return result;
        }
        public int maxDepth(TreeNode root) {
            if(root == null){
                return 0;
            }
            int l = maxDepth(root.left);
            int r = maxDepth(root.right);
            if (Math.abs(l - r) > 1){ 
                result = false;
            }
            return Math.max(l,r)+1;
        }
    }
    
    • LeetCode 110.二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
    示例:
    给定二叉树
    [1,2,3,4,5]
    返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        private int max = 0;
        public int diameterOfBinaryTree(TreeNode root) {
            maxDepth(root);
            return max;
        }
        private int maxDepth(TreeNode root) {
            if (root == null){
                return 0;
            }
            int left_depth = maxDepth(root.left);
            int right_depth = maxDepth(root.right);
            max = Math.max(max,left_depth+right_depth);
            return Math.max(left_depth,right_depth)+1;
        }
    }
    
    • LeetCode 112.路径的总和

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
    说明:叶子节点是指没有子节点的节点。
    示例:
    给定如下二叉树,以及目标和 sum = 22,
    [5,4,8,11,null,13,4,7,2,null,null,null,1]
    返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean hasPathSum(TreeNode root, int sum) {
            if(root == null){
                return false;
            }
            if(root.left == null && root.right == null && sum == root.val){
                return true;
            }
            return hasPathSum(root.left,sum - root.val) || hasPathSum(root.right,sum - root.val);
        }
    }
    
    • LeetCode 617.合并二叉树

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
    你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
            if (t1 == null && t2 == null){
               return null; 
            }
            if (t1 == null){
                return t2;
            }
            if (t2 == null) {
                return t1;
            }
            TreeNode root = new TreeNode(t1.val + t2.val);
            root.left = mergeTrees(t1.left, t2.left);
            root.right = mergeTrees(t1.right, t2.right);
            return root;
        }
    }

    相关文章

      网友评论

          本文标题:LeetCode | 树相关题目

          本文链接:https://www.haomeiwen.com/subject/nqmyeqtx.html