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, ...
随机推荐
- 6.cookie、session,localStorage、sessionStorage
必须在服务器下运行 cookie/session 存东西 cookie 时间 过期时间 4k 服务器把一部分数据保存在客户端(浏览器) session 回话 时间 服务器存取用户信息 5M local ...
- python全栈开发 * 12 知识点汇总 * 180530
12 知识点总结 装饰器进阶 ⼀. 通⽤装饰器的回顾1.开闭原则: 对增加功能开放. 对修改代码封闭2.装饰器的作⽤: 在不改变原有代码的基础上给⼀个函数增加功能3.通⽤装饰器的写法:def wrap ...
- en-zh(社会问题)social problems-2
让屏幕代替父母陪孩子?世卫组织:这样是不对的! No sedentary screen time for babies, WHO says Babies and toddlers should not ...
- Java加载dll或so库文件的路径 java.library.path
1. Java的System.load 和 System.loadLibrary都可以用来加载库文件 2.例如你可以这样载入一个windows平台下JNI库文件: System.load(&q ...
- 高性能Nginx服务器-负载均衡
Location正则表达式 location的作用 location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作. locatio ...
- vue的install
将$platform挂载到Vue.prototype下面,this里面的内容就是$platform里面的东西 Vue.prototype.$platform = this 在chrome开发者工具下的 ...
- solr6.5.1搜索引擎的部署
目录结构如下: 6.5.1版本的solr已经集成有jetty服务器(在server目录下),所以可以直接启动solr应用. 1.java环境配置好(这里不再累赘). 2.打开cmd,路径切换到bin目 ...
- Docker:从引擎和运行框架理解Docker(3)
Docker是GO语言编写的. 1.Docker发挥的作用: 1.快速.一致.标准化的交付应用.从开发.测试.到部署交付到成产环境都可以使用docker命令处理image到不同的环境 2.部署和扩展: ...
- Python requests库如何下载一个图片资源
原文地址https://blog.csdn.net/u011541946/article/details/77700074 前面一篇文章介绍了response对象的一些常用API,也已经提到,我们的重 ...
- 动物管理员--zooKeeper-01
ZooKeeper集群角色介绍: 最典型集群模式:Master/Slave 模式(主备模式).在这种模式中,通常 Master 服务器作为主服务器提供写服务,其他的 Slave 服务器从服务器通过异步 ...