LeetCode 热题 HOT 100 ------- 98. 验证二叉搜索树 104. 二叉树的最大深度(dfs,bfs) 543. 二叉树的直径

dsadadsa在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 //思路1:递归思想,这样想:左子树<当前节点<右子树,因此我们通过一个函数,让他一直满足这个条件,注意递归,先写结束条件
class Solution {
    public boolean isValidBST(TreeNode root) {

        long low = Long.MIN_VALUE;
        long high = Long.MAX_VALUE;
        return compareNode(root , low , high);
    }
    private boolean compareNode(TreeNode node , long low , long high){
        if(node == null)
            return true;
        if(node.val <= low || node.val >= high)
            return false;
        return compareNode(node.left , low , node.val) && compareNode(node.right , node.val , high);
    }
}
//思路2:考虑二叉树中序遍历(思想),只不过在遍历过程中,我们要判断当前节点比上一个节点大(因为中序遍历是左子树当前右子树)
class Solution {
    //定义前一个节点的值,初始值为最小值
    long pre = Long.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        if(!isValidBST(root.left)){  
            return false;
        }
        //访问当前节点:如果当前节点小于等于中序遍历的前一个节点,说明不满足BST,返回 false;否则继续遍历。
        if(root.val <= pre){
            return false;
        }
        //更换最小值
        pre = root.val;

        return isValidBST(root.right);
    }
}

//思路3:依旧考虑二叉树的中序遍历,只不过我们通过"栈"写,不用递归思想
class Solution {
    public boolean isValidBST(TreeNode root) {
        //定义一个栈
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        //定义一个最小值
        long pre = Long.MIN_VALUE;
        while(!stack.isEmpty()||root!=null){
            //先放入的是所有左子树节点(因为我们栈是先入后出,所以最后先输出的正好是最底层的左子树节点)
            while(root!=null){
                stack.push(root);
                root=root.left;
            }
            //取出第一个左子节点
            root = stack.pop();
            //然后我们对当前的值进行比较,如果小于上一个值,就说明不是二叉搜索树
            if(root.val<=pre){
                return false;
            }
            pre = root.val;
            //然后再去找比较右节点
            root = root.right;
        }
        //都满足以上说明是二叉搜索树
        return true;  
    }
}

dsadadsa在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

bfs
class Solution {
    public int maxDepth(TreeNode root) { 
        if(root == null)
            return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        int depth = 0;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size>0){
                TreeNode node = queue.poll();
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
                size--;
            }
            depth++;
        }
        return depth;
    }
}

//dfs
class Solution {
    public int maxDepth(TreeNode root) { 
        if(root == null){
            return 0;
        }else{
            int leftDeepth = maxDepth(root.left);
            int rightDeepth = maxDepth(root.right);
            return Math.max(leftDeepth , rightDeepth) + 1;
        }
    }
}

dsadadsa在这里插入图片描述
在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
     //所经过的节点数
    int ans;
    public int diameterOfBinaryTree(TreeNode root) {
        ans=1;
        depth(root);
        //一个节点的最大直径=路径长度的最大值=所经过的节点数-1
        return ans-1;
    }
    //通过递归函数计算,假想成随便一个结点处,求最大节点数
    public int depth(TreeNode node){
        //递归头,怎么才能结束
        if(node==null){
            return 0;
        }
        //如果不为空,则我们计算其左右子结点为根的深度
        int l = depth(node.left);
        int r = depth(node.right);
        //计算当前节点所经过的最大节点数
        ans = Math.max(ans,l+r+1);
        return Math.max(l,r)+1;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值