Lintcode482-Binary Tree Level Sum-Easy
482. Binary Tree Level Sum
Given a binary tree and an integer which is the depth of the target level.
Calculate the sum of the nodes in the target level.
Example
Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
and depth=2
Output:5
Example 2:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
and depth=3
Output:22
注意:
- sum 定义为类成员变量
代码:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: the root of the binary tree
* @param level: the depth of the target level
* @return: An integer
*/
int sum = 0;
public int levelSum(TreeNode root, int level) {
helper(root, 1, level);
return sum;
}
public void helper(TreeNode root, int depth, int level) {
if (root == null) {
return;
} if (depth == level) {
sum += root.val;
return;
} helper(root.left, depth + 1, level);
helper(root.right, depth + 1, level);
}
}
Lintcode482-Binary Tree Level Sum-Easy的更多相关文章
- 【Leetcode】【Easy】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- LeetCode107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- 好用的Quartz管理器类
转发的代码,原理就不提了 下面直接上代码: QuartzManager.java 动态添加.修改和删除定时任务管理类 import org.quartz.CronScheduleBuilder; im ...
- 剑指offer——python【第29题】最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路 先排序后取数,排序可以用冒泡,插入,选择,快排,二分法等等, ...
- python的几个小程序
##九九乘法口诀 num1=1 while num1<10: num2=1 while num2<=num1: print(num2,"*",num1,"=& ...
- java中基本类型double和对象类型Double
Double.valueOf(str)把String转化成Double类型的对象比如Stirng str="1.0";那么Double.valueOf(str)等价于new Dou ...
- 洛谷P3808 【模板】AC自动机(简单版)
题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...
- python基础(16)-进程&线程&协程
进程之multiprocessing模块 Process(进程) Process模块是一个创建进程的模块,借助这个模块,就可以完成进程的创建. 介绍 初始化参数 Process([group [, t ...
- webstorm 配置 开发微信小程序
默认情况下,webstorm是不支持wxml和wxss的文件类型,不会有语法高亮 设置高亮 除了高亮,还需要代码提示, 所幸已经有前辈整理了小程序的代码片段,只需要导入其安装包即可使用,包文件路径如下 ...
- Ch01 基础 - 练习
1. 在Scala REPL 中键入3.,然后按Tab键.有哪些方法可以被应用? scala> 3. % * - > >> ^ ...
- 8 . IO类-标准IO、文件IO、stringIO
8.1 IO类 #include <iostream> //标准IO头文件 8.2 文件输入输出流 #include <fstream> //读写文件头文件 std::fst ...
- CentOS6.10安装redis5.0
1.以安装redis5.0.0为例 下载安装包:http://redis.io 安装非常简单! [root@centos6 ~]#yum install gcc #需要先安装GCC,如果已安装请忽略 ...