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.二叉树展开为链表

相关推荐
2401_891482172 小时前
多平台UI框架C++开发
开发语言·c++·算法
平行云PVT2 小时前
数字孪生信创云渲染技术解析:从混合信创到全国产化架构
linux·unity·云原生·ue5·图形渲染·webgl·gpu算力
88号技师2 小时前
2026年3月中科院一区SCI-贝塞尔曲线优化算法Bezier curve-based optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
t198751282 小时前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab
无敌昊哥战神2 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
xdscode3 小时前
Linux云服务器安装openclaw,并对接飞书通道
linux·服务器·飞书·openclaw
x_xbx3 小时前
LeetCode:148. 排序链表
算法·leetcode·链表
Darkwanderor3 小时前
三分算法的简单应用
c++·算法·三分法·三分算法
2401_831920743 小时前
分布式系统安全通信
开发语言·c++·算法
Percep_gan3 小时前
Linux中安装Redis,很详细
linux·运维·redis