首页 > 其他分享 >感觉理解不是很透彻

感觉理解不是很透彻

时间:2022-12-27 00:44:16浏览次数:38  
标签:right return 理解 透彻 感觉 path null root left

110.平衡二叉树

    /**
     * <A href="https://leetcode.cn/problems/balanced-binary-tree/">110. 平衡二叉树</A>
     * <BR>
     */
    public boolean isBalanced(TreeNode root) {
        return Math.abs(getHeight(root.left) - getHeight(root.right)) <= 1;
    }

    public int getHeight(TreeNode cur) {
        if (cur == null) return 0;
        int left = getHeight(cur.left);
        if (left == -1) {
            return -1;
        }
        int right = getHeight(cur.right);
        if (right == -1) {
            return -1;
        }
        if (Math.abs(right - left) > 1) {
            return -1;
        }
        return Math.max(left, right) + 1;
    }

257. 二叉树的所有路径

public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<String>();
        List<Integer> path = new ArrayList<>();
        if (root == null) {
            return result;
        }
        getPath(root, path, result);
        return result;
    }

    public void getPath(TreeNode cur, List<Integer> path, List<String> result) {
        path.add(cur.val);
        String wayPath = "";
        if (cur.left == null && cur.right == null) {
            for (Integer integer : path) {
                wayPath += integer + "->";
            }
            wayPath = wayPath.substring(0, wayPath.length() - 2);
            result.add(wayPath);
            return;
        }
        if (cur.left != null) {
            getPath(cur.left, path, result);
            path.remove(path.size() - 1);
        }
        if (cur.right != null) {
            getPath(cur.right, path, result);
            path.remove(path.size() - 1);
        }
    }

404. 左叶子之和

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left==null &&root.right==null){
            return 0;
        }

        int mid = 0;
        int left = sumOfLeftLeaves(root.left);
        int right = sumOfLeftLeaves(root.right);
        if (root.left != null && (root.left.left == null && root.left.right == null)) {
            mid = root.left.val;
        }
        return mid + right + left;
    }
}

标签:right,return,理解,透彻,感觉,path,null,root,left
From: https://www.cnblogs.com/Chain-Tian/p/17007205.html

相关文章

  • 0210_【理解】Swagger安全配置
    1、//https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-securityimplementationgroup:'org.springframework.boot',name:'spring-b......
  • 0209_【理解】REST接口描述
    1、packagecom.yootk.provider.config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration; import......
  • 0208_【理解】Swagger接口描述
    1、//https://mvnrepository.com/artifact/io.springfox/springfox-boot-starterimplementationgroup:'io.springfox',name:'springfox-boot-starter',version:'3.0......
  • 同步、异步、阻塞、非阻塞---BIO、NIO、AIO的简单理解
    概念BIO:同步并阻塞,服务实现模式为一个连接对应一个线程,即客户端发送一个连接,服务端要有一个线程来处理。如果连接多了,线程数量不够,就只能等待,即会发生阻塞。NIO:同步非阻塞,服......
  • 正确理解和使用JAVA中的字符串常量池
    前言研究表明,Java堆中对象占据最大比重的就是字符串对象,所以弄清楚字符串知识很重要,本文主要重点聊聊字符串常量池。Java中的字符串常量池是Java堆中的一块特殊存储区域,用......
  • 理解 LSTM 网络
     发表于2015年8月27日 循环神经网络人类不会每一秒都从头开始思考。当你阅读这篇文章时,你会根据对之前单词的理解来理解每个单词。您不会扔掉所有东西并重新从头开......
  • 『DL笔记』深入理解softmax交叉熵损失函数反向传播求导过程分析
    目录​​一、softmax函数​​​​二、损失函数lossfunction​​​​三、最后的准备工作                         ......
  • SVM笔记1:SVM中超平面的理解!
    一、什么是超平面以上是三维为例子。 通过查阅资料对超平面有了一定的认识, 维空间中的超平面由下面的方程确定:                  ......
  • 如何理解redis两种不同的持久化方式
    其实redis就是一种高级的以键值对形式存储数据的数据库,而它的好处就是他可以支持数据的持久化,其实redis之所以会有这样的优点,主要是因为,redis的数据都是存放在内存中的,如果......
  • 面试官:说说你对 Node 中的 Stream 的理解
    大家好,我是CoderBin前言面试官:“说说你对Node中的Stream的理解”紧张的萌新:“好像是一种流?...”面试官:“...”······又来到了面试官系列,本次讲解的是node中关于......