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, ...
随机推荐
- 五子棋棋盘布局 table和ul哪个好
想要在页面布局以上棋盘,可以用ul li 布局,但却有明显的缺点: 1.两行两列:ul li 下面如果再放li 会出错,只能是放其他的,比如div. 或者放li *行*列: 但是这样有两个明显缺陷: ...
- juqery 给本身的class加上一个class 或也可以实现关注商品,取消关注商品
$("#goods1").on("click",".ICON-fen-LOVE",function(){ var $this = $(thi ...
- C语言定义的操作mysql数据库的接口
编写的环境:centos7系统下,对mysql的衍生mariadb进行数据库的操作,包含设置访问数据库的参数,查询数据库和增删改数据库的三个功能.对于查询数据库,我这里允许不返回查询结果,用于判断查询 ...
- linux中的cd
cd命令 实例 hling@hling:~$ cd /home/hling/桌面/huanghling@hling:~/桌面/huang$ cd ..hling@hling:~/桌面$ cd ..hl ...
- easyui equals验证代码
在使用easyui textbox进行相等验证时却没有效果,经查询原来官方代码中没有提供equals验证的方法,搜了一个加上去就OK了: // extend the 'equals' rule $.e ...
- 山东13年省赛 Aliceand Bob
Problem F: Alice and Bob Description Alice and Bob like playing games very much.Today, they introduc ...
- java_Arrays.sort()方法
这个方法位于util包里,可以传入任一类型数组,默认按照字典序升序排序 如果要按照降序排序,直接写一个循环来颠倒顺序就好了 源码如下 String[] name = {"1",&q ...
- CentOS7.6 安装Docker
删除已安装的Docker # Uninstall installed docker sudo yum remove docker \ docker-client \ docker-client-lat ...
- 《Mysql 用户管理》
一:数据库用户 ROOT 和 其他用户有什么区别么? - Mysql root 和 linux root 不是一回事,数据库 root 只不过是初始化时候自己建立的一个用户而已,随时可以删除/修改. ...
- 使用hashlib进行文件校验
import hashlib import os path = r'D:\CentOS 64 位' def file_md5(path): """ 文件校验 :param ...