速通Hot100-Day10——二叉树

今天这种关于二叉树中,判断是否是遍历当前节点的写法让我受学习。

不过,可能那种父亲角度理解的面试官喜欢吧。

222. 完全二叉树的节点个数

就是简单的递归求取节点,有点类似于求取树的高度了

java 复制代码
/**
 * 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 {
    public int countNodes(TreeNode root) {
        if(root == null) return 0;
        return getNode(root);
    }

    private int getNode(TreeNode cur) {
        if(cur == null) return 0;
        
        int left = getNode(cur.left);
        int right = getNode(cur.right);

        return left + right + 1;
    }
}

110. 平衡二叉树

平衡,最后需要统一判断是否左右子树高度的差值。

java 复制代码
/**
 * 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 {
    public boolean isBalanced(TreeNode root) {
        if(root == null) return true;
        return traversal(root);
    }

    private boolean traversal(TreeNode cur) {
        if(cur == null) return true;

        boolean l = traversal(cur.left);
        boolean r = traversal(cur.right);
        int left = getDepth(cur.left);
        int right = getDepth(cur.right);
        int diff = Math.abs(left - right);
        return l && r && diff <= 1;
    }

    private int getDepth(TreeNode cur) {
        if(cur == null) return 0;

        int left = getDepth(cur.left);
        int right = getDepth(cur.right);
        return Math.max(left, right) + 1;
    }
}

257. 二叉树的所有路径

就是递归,理解递归的条件、出口、参数即可。

java 复制代码
/**
 * 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 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> paths = new ArrayList<>();
        if(root == null) return paths;

        dfs(root, "", paths);
        return paths;
    }

    private void dfs(TreeNode cur, String path, List<String> paths) {
        if(path.isEmpty()) {
            path = cur.val + "";
        } else {
            path = path + "->" + cur.val;
        }

        if(cur.left == null && cur.right == null) {
            paths.add(path);
            return;
        }

        if(cur.left != null) dfs(cur.left, path, paths);
        if(cur.right != null) dfs(cur.right, path, paths);
    }
}

112. 路径总和

这里存储两个队列,到当前节点就计算当前的总和totalSum,并非提前计算,所以会清楚一点。

那么要求一开始 sum 初始值 0;

如果想要提前计算,那么sum 初始值是 root.val

java 复制代码
/**
 * 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 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;

        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);
        LinkedList<Integer> sum = new LinkedList<>();
        sum.offer(0);

        while(!q.isEmpty()) {
            int curSize = q.size();
            while(curSize -- > 0) {
                TreeNode t = q.poll();
                int curSum = sum.poll();

                int totalSum = curSum + t.val;

                if(t.left == null && t.right == null && totalSum == targetSum) return true;
                if(t.left != null) {
                    q.offer(t.left);
                    sum.offer(totalSum);
                }
                if(t.right != null) {
                    q.offer(t.right);
                    sum.offer(totalSum);
                }
            }
        }
        return false;
    }
}

404. 左叶子之和

这个节点有点是站在父亲节点的角度去判断的,是否是左节点,然后是叶子节点。

java 复制代码
/**
 * 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 {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) return 0;
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);

        int res = 0;
        while(!q.isEmpty()) {
            int curSize = q.size();
            for(int i = 0;i < curSize;i++) {
                TreeNode t = q.poll();
                if(t.left != null && t.left.left == null
                && t.left.right == null) res += t.left.val;
                if(t.left != null) q.offer(t.left);
                if(t.right != null) q.offer(t.right);
            }
        }

        return res;
    }
}

那么还有一种是,遍历当前节点才判断是否是左节点了。

java 复制代码
/**
 * 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 {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null) return 0;
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);
        LinkedList<Boolean> isLeft = new LinkedList<>();
        isLeft.offer(false);

        int res = 0;
        while(!q.isEmpty()) {
            int curSize = q.size();
            for(int i = 0;i < curSize;i++) {
                TreeNode t = q.poll();
                boolean flag = isLeft.poll();

                if(flag && t.left == null && t.right == null) res += t.val;

                if(t.left != null) {
                    q.offer(t.left);
                    isLeft.offer(true);
                }
                if(t.right != null) {
                    q.offer(t.right);
                    isLeft.offer(false);
                }
            }
        }

        return res;
    }
}

513. 找树左下角的值

这个层序,先加入右节点,再左节点,那么最后一个元素必然是个左下角。

所以只需要让 res 一直赋值即可。

java 复制代码
/**
 * 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 {
    public int findBottomLeftValue(TreeNode root) {
        if(root == null) return 0;
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);

        int res = 0;
        while(!q.isEmpty()) {
            int curSize = q.size();
            while(curSize-- > 0) {
                TreeNode t = q.poll();
                res = t.val;
                if(t.right != null) q.offer(t.right);
                if(t.left != null) q.offer(t.left);
            }
        }
        
        return res;
    }
}

如果是正常层序,那么就需要判断当前是否是第一个节点,那么才是左下角的点。

java 复制代码
/**
 * 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 {
    public int findBottomLeftValue(TreeNode root) {
        if(root == null) return 0;
        LinkedList<TreeNode> q = new LinkedList<>();
        q.offer(root);

        int res = 0;
        while(!q.isEmpty()) {
            int curSize = q.size();
            for(int i = 0;i < curSize;i++){
                TreeNode t = q.poll();
                if(i == 0) res = t.val;
                if(t.left != null) q.offer(t.left);
                if(t.right != null) q.offer(t.right);
                
            }
        }
        
        return res;
    }
}
相关推荐
码流子9 分钟前
2026 图像数据标注工具横评:LabelImg / CVAT / Label Studio / X-AnyLabeling / 国产Web平台,到底怎么选?
大数据·人工智能·算法
hansang_IR10 分钟前
【题解】[JSOI2016] 最佳团体(分数规划)
c++·算法
羊羊小栈25 分钟前
基于「YOLO目标检测 + 多模态AI分析」的公共场所暴力安全智能检测分析预警系统
人工智能·算法·面试·毕业设计·大作业
土星云SaturnCloud35 分钟前
ResNet-50 图像分类算法在边缘微服务器上的部署与性能评测
服务器·人工智能·算法·边缘计算·resnet-50
Niuguangshuo37 分钟前
文本-音频时间戳对齐:5 个开源工具实测
算法·开源·音视频
Niuguangshuo41 分钟前
论文解读:RNN-Transducer,端到端流式 ASR 的骨架
算法·语音识别
GreenTea1 小时前
🔥别再用压缩了,Codex、NVIDIA、DeepSeek、Uber 给出 Agent 上下文管理的新答案
前端·后端·算法
All for pursuit.1 小时前
【矩阵-2】240.搜索二维矩阵 II
数据结构·c++·算法·leetcode
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 209. 长度最小的子数组 | C++ 滑动窗口(毛毛虫算法)经典模板
c++·算法·leetcode
6Hzlia2 小时前
【Classic 150 刷题计划】 LeetCode 28. 找出字符串中第一个匹配项的下标 | C++ 滑动窗口与子串比对
c++·算法·leetcode