题目 404. 左叶子之和

如题

题解

  • 类似树的遍历的递归
  • 注意一定要是叶子结点

代码

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

题目 938. 二叉搜索树的范围和

题解

  • 递归

代码

class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
if(root == null){return 0;} if(root.val>=L&&root.val<=R){
return root.val+rangeSumBST(root.left,L,R)+rangeSumBST(root.right,L,R);
}else if(root.val<=L){
return rangeSumBST(root.right,L,R);
}else{
return rangeSumBST(root.left,L,R);
}
}
}

[LeetCode]404. 左叶子之和(递归)、938. 二叉搜索树的范围和(递归)(BST)的更多相关文章

  1. LeetCode 404. 左叶子之和(Sum of Left Leaves)

    404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...

  2. Java实现 LeetCode 404 左叶子之和

    404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...

  3. 【LeetCode】404. 左叶子之和

    404. 左叶子之和 知识点:二叉树 题目描述 计算给定二叉树的所有左叶子之和.. 示例 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解 ...

  4. LeetCode初级算法--树02:验证二叉搜索树

    LeetCode初级算法--树02:验证二叉搜索树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  5. LeetCode: 404.左叶子节点

    计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父 ...

  6. [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)

    题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...

  7. LeetCode 938. 二叉搜索树的范围和

    题目链接:https://leetcode-cn.com/problems/range-sum-of-bst/ 给定二叉搜索树的根结点 root,返回 L 和 R(含)之间的所有结点的值的和. 二叉搜 ...

  8. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

随机推荐

  1. fatal: 远程 origin 已经存在 | 关于git push 详解

    fatal: 远程 origin 已经存在. 解决方法1:删除origin主机名 git remote rm origin #删除 git remote add origin https://gith ...

  2. Python日期时间(详细)

    获取当前时间戳 import time t = time.time() millis1 = int(t) print('10位时间戳:{}'.format(millis1)) millis2 = in ...

  3. 【Net】StreamWriter.Write 的一点注意事项

    背景 今天在维护一个旧项目的时候,看到一个方法把string 转换为 byte[] 用的是写入内存流的,然后ToArray(),因为平常都是用System.Text.Encoding.UTF8.Get ...

  4. 5.oracle用户管理

    一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码;  ...

  5. ceph 开启mgr balancer

    参考链接: mgr balancer模式探索及配置方法1 mgr balancer模式探索及配置方法2 1.ceph mgr module enable balancer [root@controll ...

  6. Mybatis动态语句

    If元素If元素是简单的条件判断逻辑,满足制定条件时追加if元素的SQL,不满足条件时不追加,使用格式如下: <select ….> SQL语句1 <if test=“条件表达式”& ...

  7. SecureCRT 关键字高亮显示

    grep命令红色高亮关键字 1.  左边侧栏 Session Manage 右键Sessions --Properties 2 Terminal--Appearance Current color s ...

  8. spring4.1及以下跨域配置

    springMVC4.1及以下,就需要对该请求配置filter,,设置请求头可支持跨域 1.web.xml配置 <!-- 跨域问题解决 --> <filter> <fil ...

  9. SDWebImage 清除磁盘缓存机制 iOS

    分析的版本 pod 'SDWebImage', '~> 5.0.6' SDWebImage默认清除磁盘缓存的时长是7天. /** * The maximum length of time to ...

  10. Spring Security如何优雅的增加OAuth2协议授权模式

    一.什么是OAuth2协议? OAuth 2.0 是一个关于授权的开放的网络协议,是目前最流行的授权机制. 数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据.系统从而产生一个短期的进入令 ...