CleanCodeHandbook Chapter 4: Binary Tree(25-32)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011732358/article/details/86441122

Binary Tree

leetcode98. Validate Binary Search Tree

题目链接
题目:给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
在这里插入图片描述
思路: 按照中序遍历每个节点,并把值保存在list中,如果有序则返回true。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> list = new ArrayList<>();
    public boolean isValidBST(TreeNode root) {
        //二叉查找树,按照中序遍历 它应该是有序的
        search(root);
        int len = list.size();
        for(int i = 1; i < len; i++){
            if(list.get(i - 1) >= list.get(i)){
                return false;
            }
        }
        return true;
    }
    public void search(TreeNode root){
       if(root != null){
            search(root.left);
            list.add(root.val);
            search(root.right);
        } 
    }
}

leetcode104. Maximum Depth of Binary Tree

题目链接
题目:给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.
思路: 比比左子树和右子树哪个高,然后返回较高的那个即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root != null){
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return (left > right ? left : right)+ 1;
        }
        return 0;
    }
}

或者

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root != null){
            return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
        }
        return 0;
    }
}

leetcode104. Maximum Depth of Binary Tree

题目链接
题目:
给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最小深度 2.
思路: 首先要明确,如果该节点是叶子节点,则返回1,若该节点的一个子树为空 另一个子树非空,在需要返回非空子树的深度,若左右子树都非空,则比左子树和右子树哪个低,然后返回较低的那个即可。


/**
 * 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;
        }
        if(root.left == null){
            return minDepth(root.right) + 1;
        }
        if(root.right == null){
            return minDepth(root.left) + 1;
        }
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }
}

猜你喜欢

转载自blog.csdn.net/u011732358/article/details/86441122