day15 | 110.平衡二叉树 257. 二叉树的所有路径 404.左叶子之和 222.完全二叉树的节点个数

代码随想录算法训练营第十五天| 110.平衡二叉树 257. 二叉树的所有路径 404.左叶子之和 222.完全二叉树的节点个数

Leetcode 110.平衡二叉树
题目链接:https://leetcode.cn/problems/balanced-binary-tree/description/

给定一个二叉树,判断它是否是 平衡二叉树

示例 1:

img

输入:root = [3,9,20,null,null,15,7]
输出:true

示例 2:

img

输入:root = [1,2,2,3,3,null,null,4,4]
输出:false

示例 3:

输入:root = []
输出:true

提示:

  • 树中的节点数在范围 [0, 5000]
  • -104 <= Node.val <= 104

图示:
在这里插入图片描述

代码:递归
/**
 * 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) {
        // 高度差不超过一即为平衡二叉树
        return getHeightTree(root)!=-1;
    }
    // 确定递归输入值,及其返回值
    public int getHeightTree(TreeNode root){
        // 确定终止条件
        if(root == null){
            return 0;
        } 
        // 单层递归逻辑
        int left = getHeightTree(root.left);
        int right = getHeightTree(root.right);
        if(left == -1) return -1;
        if(right == -1) return -1;
        // 当高度差大于一时
        if(Math.abs(left - right) > 1) return -1;
        // 当高度差等于一时
        return Math.max(left,right) + 1;
    }  
}
Leetcode 257. 二叉树的所有路径
题目链接:https://leetcode.cn/problems/binary-tree-paths/submissions/548430673/

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。

叶子节点 是指没有子节点的节点。

示例 1:

img

输入:root = [1,2,3,null,5]
输出:["1->2->5","1->3"]

示例 2:

输入:root = [1]
输出:["1"]

提示:

  • 树中节点的数目在范围 [1, 100]
  • -100 <= Node.val <= 100
思路:

递归、迭代法

代码1:递归
/**
 * 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<Integer> paths = new ArrayList<>();
        // 存最后的结果
        List<String> res = new ArrayList<>();
        // 判断递归终止条件
        if(root == null) return res;
        travsel(root,paths,res);
        return res;
    }
    // 确定递归的参数和返回值
    public void travsel(TreeNode root , List<Integer> paths , List<String> res) {
        // 单层递归逻辑

        // 前序遍历

        // 中
        paths.add(root.val);
        // 确定遍历到叶子结点
        if(root.left == null && root.right == null){
            // 输出路径
            // 定义StringBuilder输出
            StringBuilder sb = new StringBuilder();
            // 遍历
            for(int i = 0 ; i<paths.size()-1;i++){
                sb.append(paths.get(i)).append("->");
            }
            // 添加下一个节点
            sb.append(paths.get(paths.size() - 1));
            // 收集路径
            res.add(sb.toString());
            return;
        }
        
        // 递归和回溯
        if(root.left!=null){
            // 左
            travsel(root.left,paths,res);
            // 回溯
            paths.remove(paths.size() - 1);
        }
        if(root.right != null){
            // 右
            travsel(root.right,paths,res);
            // 回溯
            paths.remove(paths.size() - 1);
        }
    }
    

    
}
代码2:迭代法
/**
 * 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> res = new ArrayList<>();
        if(root == null) return res;
        // 声明栈
        Stack<Object> stack = new Stack<>();
        stack.push(root);
        stack.push(root.val + "");
        while(!stack.isEmpty()){
            // 路径与节点同时出栈
            String path = (String) stack.pop();
            TreeNode node = (TreeNode) stack.pop();
            // 如果是叶子结点
            if(node.left == null && node.right == null){
                res.add(path);
            }
            // 如果左节点不为空
            if(node.left != null){
                stack.push(node.left);
                stack.push(path + "->" + node.left.val);
            }
            //右节点不为空
            if(node.right != null){
                stack.push(node.right);
                stack.push(path + "->" + node.right.val);
            }
        }
        return res;
    }
}
总结:

注意回溯过程

Leetcode 404.左叶子之和
题目链接:https://leetcode.cn/problems/sum-of-left-leaves/description/

给定二叉树的根节点 root ,返回所有左叶子之和。

示例 1:

img

输入: root = [3,9,20,null,null,15,7] 
输出: 24 
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

示例 2:

输入: root = [1]
输出: 0

提示:

  • 节点数在 [1, 1000] 范围内
  • -1000 <= Node.val <= 1000
思路:

1、递归

2、迭代

代码1:递归
/**
 * 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) {
        // 递归
        
        return leftSum(root);

    }
    // 确定参数和返回值
    public int leftSum(TreeNode root){
        // 单层递归逻辑 
        // 因为需要向上一层返回sum,所以采用后序遍历
        // 终止条件
        if(root == null) return 0;
        int leftValue = leftSum(root.left);
        int rightValue = leftSum(root.right);
        int midValue = 0;
        // 寻找叶子结点
        if(root.left!=null &&root.left.left == null && root.left.right == null){
            midValue = root.left.val;
        }
        // 计算总和
        int sum = leftValue + rightValue + midValue;
        return sum;
    }
    
   
}
代码2:迭代
/**
 * 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;
        Stack<TreeNode> stack = new Stack<>();
        int res = 0;
        // add是继承自Vector的方法,且返回值类型是boolean。
        // push是Stack自身的方法,返回值类型是参数类型。
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            // 此时的node为左叶子节点的上一个节点
            if(node.left != null && node.left.left == null && node.left.right == null){
                res += node.left.val;
            }
            if(node.left != null) stack.push(node.left);
            if(node.right != null) stack.push(node.right);
        } 
        return  res;
    }
}
总结:

注意计算的是左叶子节点之和,而不是左叶子之和

Leetcode 222.完全二叉树的节点个数
题目链接:https://leetcode.cn/problems/count-complete-tree-nodes/description/
  • 给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

    完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

    示例 1:

    img

    输入:root = [1,2,3,4,5,6]
    输出:6
    

    示例 2:

    输入:root = []
    输出:0
    

    示例 3:

    输入:root = [1]
    输出:1
    

    提示:

    • 树中节点的数目范围是[0, 5 * 104]
    • 0 <= Node.val <= 5 * 104
    • 题目数据保证输入的树是 完全二叉树

    **进阶:**遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗?

思路:

1、利用普通二叉树特性(递归)

2、利用满二叉树特性计算完全二叉树(递归)

3、迭代法

图示:

在这里插入图片描述

代码1:利用普通二叉树特性(递归)
/**
 * 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 countNodes(root.left) + countNodes(root.right) + 1;
        // 1 为本身节点的数量
    }
}
代码2:利用满二叉树特性计算完全二叉树(递归)
/**
 * 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) {
        // 利用满二叉树的特性
        // 递归
        return count(root);
    }
    // 确定递归返回值和参数
    public int count(TreeNode root){
        if(root == null) return 0;
        int leftValue = 0;
        int rightValue = 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        while(left != null) {
            // 求左子树深度
            left = left.left;
            leftValue++;
            
        }
        while(right != null){
            // 求右子树深度
            right = right.right;
            rightValue++;
          
        }
        // 比较左右子树的深度
        if(leftValue == rightValue){
             return (2 << leftValue) - 1; 
             // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return count(root.left) + count(root.right) + 1;
    }
}
代码3:迭代法
/**
 * 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;
        Queue<TreeNode> queue = new LinkedList<>();
        int result = 0 ;
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size -- > 0){
                TreeNode cur = queue.poll();
                result++;
                if(cur.left!=null) queue.offer(cur.left);
                if(cur.right!=null) queue.offer(cur.right);
            }   
        }
        return result;
    }
}
总结:

满二叉树计算公式2^k - 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值