LeetCode热题100记录-【二叉树】

二叉树

94.二叉树的中序遍历

记录:不需要二刷,秒

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        traverse(root);
        return res;
    }
    List<Integer> res = new LinkedList<>();
    void traverse(TreeNode root){
        if(root == null){
            return;
        }
        traverse(root.left);
        res.add(root.val);
        traverse(root.right);
    }
}

104.二叉树的最大深度

记录:不需要二刷,秒

java 复制代码
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int maxLeft = maxDepth(root.left);
        int maxRight = maxDepth(root.right);
        return Math.max(maxLeft,maxRight)+1;   
    }
}

226.翻转二叉树

记录:不需要二刷,秒

java 复制代码
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return root;
        }
        TreeNode leftTree = invertTree(root.left);
        TreeNode rightTree = invertTree(root.right);
        root.left = rightTree;
        root.right = leftTree;
        return root;
    }
}

101.对称二叉树

思考:不要跳进递归。站在一个节点root上看,怎么判断它是否对称?左右节点是否相等。左右节点如果有个为空怎么办?判断是否都为空,都为空则true,否则false。然后递归这个root节点的左子树,右子树,判断是否对称

牢记分解问题的思想 --- 抽出一个节点单独看

记录:需要二刷

java 复制代码
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
      return _isSymmetric(root.left,root.right);
    }
    boolean _isSymmetric(TreeNode root1,TreeNode root2){
        if(root1 == null || root2 == null){
            return root1 == root2;
        }
        if(root1.val != root2.val){
            return false;
        }
        return _isSymmetric(root1.left,root2.right) && _isSymmetric(root1.right,root2.left);
    }
}

543.二叉树的直径

思考:在计算leftMax和rightMax时顺便计算一下二叉树的直径

记录:需要二刷

java 复制代码
class Solution {
    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return res;
    }
    int res = 0;
    int maxDepth(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftMax = maxDepth(root.left);
        int rightMax = maxDepth(root.right);
        res = Math.max(leftMax+rightMax,res);
        return Math.max(leftMax,rightMax)+1;
    }
}

102.二叉树的层序遍历

思路:用一个队列依次存入当前遍历到的节点root,然后再将这个节点拿出来存到这一层track里,再把下一层root存入队列里,这一层遍历完把这一层track存到res里

记录:需要二刷

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            List<Integer> track = new ArrayList<>();
            for(int i = 0;i < size;i++){
                TreeNode cur = q.poll();
                track.add(cur.val);
                if(cur.left != null){
                    q.offer(cur.left);
                }
                if(cur.right != null){
                    q.offer(cur.right);
                }
            }
            res.add(track);
        }
        return res;
    }
}

108.将有序数组转换为二叉搜索树

思考:BST的核心就是中序遍历就是升序数组,这样每次把数组最中间的元素作为root,再分解构造即可

记录:需要二刷

java 复制代码
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length == 0){
            return null;
        }
        return buildBST(nums,0,nums.length-1);
    }
    TreeNode buildBST(int[] nums,int start,int end){
        if(start > end){
            return null;
        }
        int middle = (start + end) / 2;
        TreeNode root = new TreeNode(nums[middle]);
        root.left = buildBST(nums,start,middle-1);
        root.right = buildBST(nums,middle+1,end);
        return root;
    }
}

98.验证二叉搜索树

思考:构造一个验证函数,每次限制上下限,递归判断

记录:需要二刷

java 复制代码
class Solution {
    public boolean isValidBST(TreeNode root) {
        return _isValidBST(root,null,null);
    }
    boolean _isValidBST(TreeNode root,TreeNode min,TreeNode max){
        if(root == null){
            return true;
        }
        if(min != null && min.val >= root.val){
            return false;
        }
        if(max != null && max.val <= root.val){
            return false;
        }
        return _isValidBST(root.left,min,root) && _isValidBST(root.right,root,max);
    }
}

230.二叉搜索树中第K小的元素

思考:直接遍历这个搜索树,中序遍历就是顺序数组,然后拿这个数组的第k个元素就行,则用rank变量记录

记录:不需要二刷

java 复制代码
class Solution {
    public int kthSmallest(TreeNode root, int k) {
        traverse(root,k);
        return res;
    }
    int res = 0;
    int rank = 0;
    void traverse(TreeNode root,int k){
        if(root == null){
            return;
        }
        traverse(root.left,k);
        rank++;
        if(rank == k){
            res = root.val;
        }
        traverse(root.right,k);
    }
}

199.二叉树的右视图

思考:思路很简单就是层序遍历,每次拿最这一层最右边的树到结果。注意的是,层序遍历时先加root.right再加root.left,这样每次peek时是最右侧元素

记录:需要二刷

java 复制代码
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            TreeNode last = q.peek();
            for(int i = 0;i < size;i++){
                TreeNode cur = q.poll();
                if(cur.right != null){
                    q.offer(cur.right);
                }
                if(cur.left != null){
                    q.offer(cur.left);
                }
            }
            res.add(last.val);
        }
        return res;
    }
}

114.二叉树展开为链表

相关推荐
子豪-中国机器人34 分钟前
2025年5月18日蓝桥stema省选拔赛编程题答案解析
c++·算法·数学建模
极光雨雨35 分钟前
【算法】回溯法
算法
君鼎44 分钟前
排序算法——详解
数据结构·算法·排序算法
岁忧1 小时前
LeetCode 高频 SQL 50 题(基础版) 之 【高级查询和连接】· 上
数据库·sql·leetcode
Johny_Zhao1 小时前
Burp Suite 企业级深度实战教程
linux·网络·网络安全·信息安全·云计算·shell·burp suite·系统运维·itsm
Felven1 小时前
A. AvtoBus
算法
良辰美景好时光1 小时前
keepalived定制日志bug
linux·运维·bug
s_little_monster1 小时前
【Linux】网络--网络层--IP协议
linux·运维·网络·经验分享·笔记·学习·tcp/ip
卫青~护驾!1 小时前
c++数据结构8——二叉树的性质
数据结构·算法
forward_huan1 小时前
wsl安装linux
linux·wsl