首页 > 其他分享 >代码随想录训练营|Day 17|110,257,404

代码随想录训练营|Day 17|110,257,404

时间:2022-10-07 04:55:05浏览次数:85  
标签:paths return 17 随想录 节点 null root Day left

110. Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of every node differ in height by no more than 1.

Example 1:

https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • 104 <= Node.val <= 104

分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了。

class Solution {
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }

    private int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if (leftHeight == -1) {
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if (rightHeight == -1) {
            return -1;
        }
        // 左右子树高度差大于1,return -1表示已经不是平衡树了
        if (Math.abs(leftHeight - rightHeight) > 1) {
            return -1;
        }
        return Math.max(leftHeight, rightHeight) + 1;
    }
}

Time Complexity:O(n)
Space Complexity:O(log n)

For Future References

题目链接:https://leetcode.com/problems/balanced-binary-tree/

文章讲解:https://programmercarl.com/0110.平衡二叉树.html#题外话

视频讲解:https://www.bilibili.com/video/BV1Ug411S7my/


257. Binary Tree Paths

Given the root of a binary tree, return all root-to-leaf paths in any order.

leaf is a node with no children.

Example 1:

https://assets.leetcode.com/uploads/2021/03/12/paths-tree.jpg

Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]

Example 2:

Input: root = [1]
Output: ["1"]

Constraints:

  • The number of nodes in the tree is in the range [1, 100].
  • 100 <= Node.val <= 100
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        List<Integer> paths = new ArrayList<>();
        traversal(root, paths, res);
        return res;
    }

    private void traversal(TreeNode root, List<Integer> paths, List<String> res) {
        paths.add(root.val);
        // 叶子结点
        if (root.left == null && root.right == null) {
            // 输出
            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) {
            traversal(root.left, paths, res);
            paths.remove(paths.size() - 1);// 回溯
        }
        if (root.right != null) {
            traversal(root.right, paths, res);
            paths.remove(paths.size() - 1);// 回溯
        }
    }
}

For Future References

题目链接:https://leetcode.com/problems/binary-tree-paths/

文章讲解:https://programmercarl.com/0257.二叉树的所有路径.html#递归

视频讲解:https://www.bilibili.com/video/BV1ZG411G7Dh/


404. Sum of Left Leaves

Given the root of a binary tree, return the sum of all left leaves.

leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

Example 1:

https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg

Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.

Example 2:

Input: root = [1]
Output: 0

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • 1000 <= Node.val <= 1000

节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点
必须要通过节点的父节点来判断其左孩子是不是左叶子。

该节点的左节点不为空,该节点的左节点的左节点为空,该节点的左节点的右节点为空

当遇到左叶子节点的时候,记录数值,然后通过递归求取左子树左叶子之和,和 右子树左叶子之和,相加便是整个树的左叶子之和。

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        int leftValue = sumOfLeftLeaves(root.left);    // 左
        int rightValue = sumOfLeftLeaves(root.right);  // 右
        int midValue = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) { 
            midValue = root.left.val;
        }
        int sum = midValue + leftValue + rightValue;  // 中
        return sum;
    }
}

For Future References

题目链接:https://leetcode.com/problems/sum-of-left-leaves/

文章讲解:https://programmercarl.com/0404.左叶子之和.html

视频讲解:https://www.bilibili.com/video/BV1GY4y1K7z8/

标签:paths,return,17,随想录,节点,null,root,Day,left
From: https://www.cnblogs.com/bluesociety/p/16759014.html

相关文章

  • Day15 SQL巩固学习
    SQL语法学习大二学习的数据库到现在已经快忘的差不多了,只会一些简单的增删改查,groupbyorderby等都忘记了,决定重新复习一些SQL语法ORDERBY该ORDERBY关键字用于按......
  • 实验4:开源控制器实践——OpenDayLight
    实验4:开源控制器实践——OpenDaylight基本要求Mininet拓扑生成并连接控制器的结果用OpenDayLight观察效果Postman中发生硬延时个人总结本次实验刚开始一直打......
  • 172. 阶乘后的零
    172.阶乘后的零给定一个整数n,返回n!结果中尾随零的数量。提示 n!=n*(n-1)*(n-2)*...*3*2*1 示例1:输入:n=3输出:0解释:3!=6,不含尾随......
  • day06-多表查询02
    多表查询024.表复制自我复制数据(蠕虫复制)有时,为了对某个sql语句进行效率测试,我们需要海量数据时,可以用此法为表创建海量数据--为了对某个sql语句进行效率测试,我们需......
  • Java多线程(day2—重要关键词)
    Java多线程中的几个关键词Synchronized与ReentrantLock SynchronizedReentrantLock层次JVM层面的锁,是Java关键词JDK提供的,属于API层面的锁使用1.修饰实......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境......
  • 实验4:开源控制器实践——OpenDaylight
    一.基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器。2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10......
  • 前端Axios-Day44
    JSONServer:模拟服务器环境插件1.进行全局安装:npmi-gjson-server2.创建db.json文件并写入相关数据:{"posts":[{"id":1,"title":"json-server","author......
  • 实验4:开源控制器实践——OpenDaylight
    实验4:开源控制器实践——OpenDaylight一、实验目的1.能够独立完成OpenDaylight控制器的安装配置;2.能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环......
  • HDU4417 Super Mario (主席树)
    主席树另一模板。查询的是[L,R]中<=h的个数。1#include<bits/stdc++.h>2usingnamespacestd;3#definelctr[i].ch[0]4#definerctr[i].ch[1]5#define......