代码随想录二刷第二十一天 | 222.完全二叉树的节点个数、110.平衡二叉树、257. 二叉树的所有路径、404. 左叶子之和、513.找树左下角的值

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

题目:222. 完全二叉树的节点个数 - 力扣(LeetCode)

题解:代码随想录

状态:AC

思路

可以用简单的前序递归遍历实现,也可以用完全二叉树的性质实现。

下面给出完全二叉树性质代码

代码

时间复杂度:O(logN × logN) 空间复杂度:O(logN)

java 复制代码
class Solution {
    public int countNodes(TreeNode root) {
        if(root == null) return 0;
        int leftDepth = 1, rightDepth = 1;
        TreeNode left = root.left, right = root.right;
        while(left != null){
            left = left.left;
            leftDepth++;
        }
        while(right != null){
            right = right.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth){
            return (int) Math.pow(2, leftDepth) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

110.平衡二叉树

题目:110. 平衡二叉树 - 力扣(LeetCode)

题解:代码随想录

状态:需要多复习

思路

平衡二叉树是左右子树高度不超过1

注意区分深度和高度,深度是从上往下,高度是从下往上

  1. 先求左右子树的高度,如果左右子树不为平衡二叉树,就返回-1
  2. 再对比左右子树高度差是否小于1,否则返回-1
  3. 父节点的高度 = 左右子树高度的最大值 + 1

代码

时间复杂度:O(N) 空间复杂度:O(N)

java 复制代码
class Solution {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }
    
    private int getHeight(TreeNode node){
        if(node == null) return 0;
        int leftHeight = getHeight(node.left);
        if(leftHeight == -1) return -1;
        int rightHeight = getHeight(node.right);
        if(rightHeight == -1) return -1;
        if(Math.abs(leftHeight - rightHeight) > 1) return -1;
        return Math.max(leftHeight, rightHeight) + 1;
    }
}

257. 二叉树的所有路径

题目:257. 二叉树的所有路径 - 力扣(LeetCode)

题解:代码随想录

状态:AC

思路

简单的回溯题

代码

时间复杂度:O(N) 空间复杂度:O(N)

java 复制代码
class Solution {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        func(root, "");
        return res;
    }
    private void func(TreeNode node, String str){
        if(node == null) return;
        if(node.left == null && node.right == null){
            res.add(str + node.val);
            return;
        }
        str += node.val + "->";
        func(node.left, str);
        func(node.right, str);
    }
}

404. 左叶子之和

题目:404. 左叶子之和 - 力扣(LeetCode)

题解:代码随想录

状态:取巧没想到

思路

如果该节点的左节点不为空,该节点的左节点的左节点为空,该节点的左节点的右节点为空,则找到了一个左叶子

代码

时间复杂度:O(N) 空间复杂度:O(N)

java 复制代码
class Solution {
    int sum = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null){
            return sum;
        }
        if(root.left != null && root.left.left == null && root.left.right == null){
            sum += root.left.val;
        }
        sumOfLeftLeaves(root.left);
        sumOfLeftLeaves(root.right);
        return sum;
    }
}

513.找树左下角的值

题目:513. 找树左下角的值 - 力扣(LeetCode)

题解:代码随想录

状态:AC

思路

层序遍历

代码

时间复杂度:O(N) 空间复杂度:O(N)

java 复制代码
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        Deque<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int size = q.size();
            res = q.peek().val;
            while(size > 0){
                TreeNode node = q.poll();
                if(node.left != null) q.offer(node.left);
                if(node.right != null) q.offer(node.right);
                size--;
            }
        }
        return res;
    }
}

112. 路径总和

题目:112. 路径总和 - 力扣(LeetCode)

题解:代码随想录

状态:AC

思路

回溯计算,注意考虑只有根节点的情况,并且只有到叶子节点的时候才判断和是否满足条件。

代码

时间复杂度:O(N) 空间复杂度:O(N)

java 复制代码
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
         if(root == null) return false;
         return deal(root, targetSum, 0);
    }
    private boolean deal(TreeNode node, int targetSum, int sum){
         if(node == null) return false;
         sum += node.val;
         if(sum == targetSum && node.left == null && node.right == null) return true;
         return deal(node.left, targetSum, sum) || deal(node.right, targetSum, sum);
    }
}
相关推荐
root_zhb11 小时前
List.contains踩坑
java·list
曾经的三心草11 小时前
Java数据结构-List-栈-队列-二叉树-堆
java·数据结构·list
Moe48812 小时前
合并Pdf、excel、图片、word为单个Pdf文件的工具类(技术点的选择与深度解析)
java·后端
Moe48812 小时前
合并Pdf、excel、图片、word为单个Pdf文件的工具类(拿来即用版)
java·后端
oliveira-time12 小时前
原型模式中的深浅拷贝
java·开发语言·原型模式
进阶的猿猴12 小时前
easyExcel实现单元格合并
java·excel
小许学java13 小时前
MySQL-触发器
java·数据库·mysql·存储过程·触发器
JEECG低代码平台13 小时前
【2025/11】GitHub本月热度排名前十的开源Java项目
java·开源·github
百***860513 小时前
Spring BOOT 启动参数
java·spring boot·后端
跟着珅聪学java13 小时前
Spring Boot 中整合 MySQL 并打印 SQL 日志
java·spring boot