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, ...
随机推荐
- 18.jwt加密
jwt 官网https://jwt.io/ jwt:json web token jwt-simple: https://www.npmjs.com/package/jwt-simple jsonwe ...
- Logcat
logcat -- "-s"选项 : 设置输出日志的标签, 只显示该标签的日志; -- "-f"选项 : 将日志输出到文件, 默认输出到标准输出流中, -f 参 ...
- 四则运算第三次 PSP
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- PostgreSQL 自动输入密码(转)
原文:https://www.cnblogs.com/litmmp/p/5122534.html 在 Shell 命令行中,使用 postgresql-client 连接 PostgreSQL 数据库 ...
- Spark入门到精通--(第一节)Spark的前世今生
最近由于公司慢慢往spark方面开始转型,本人也开始学习,今后陆续会更新一些spark学习的新的体会,希望能够和大家一起分享和进步. Spark是什么? Apache Spark™ is a fast ...
- java框架之SpringBoot(13)-检索及整合Elasticsearch
ElasticSearch介绍 简介 我们的应用经常需要使用检索功能,开源的 Elasticsearch 是目前全文搜索引擎的首选.它可以快速的存储.搜索和分析海量数据.SpringBoot 通过整合 ...
- Excel编辑栏字体显示大小
文件-> 选项-> 常规与保存-> 标准字体-> 确定 关闭文件,重启生效
- ORA-39006错误原因及解决办法
使用impdp导出数据时碰到ora-39006错误,错误提示如下所示: ORA-39006: internal error ORA-39213: Metadata processing is not ...
- 用maven创建一个web项目
下面所使用的Eclipse开发工具为Eclipse Java EE IDE 版本. 1.创建一个maven项目,如图所示: 选择“maven-archetype-webapp”,如图所示: 后面几步按 ...