Path Sum
来源:互联网 发布:网络用语pu是什么意思 编辑:程序博客网 时间:2024/04/29 11:47
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
bool hasPathSum(TreeNode *root, int sum) {if(root == NULL) return false;if(root->right == NULL && root->left == NULL && root->val == sum) return true;else return (hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val));}
- Path Sum && Path Sum ||
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Android延时执行方法
- linphone-android移植
- Linux中C语言open函数打开或创建文件详细讲解
- 《火球——UML大战需求分析》(第1章 大话UML)——1.3 行为型的UML(Behavior Diagram)
- 自动化测试 + QTP 11 破解方法
- Path Sum
- 三:两级节点的树,展开,无异步
- Tomcat Server.xml配置文件
- Java程序遇到的问题, 不断更新中
- 如何削减oracle的回滚空间
- ARM926EJ-S/ARM920T 协处理器 CP14, CP15详解
- win32 opengl example - 01
- window.print打印网页
- ubuntu11.10下编译linphone-android的2012最新版本