Path Sum
来源:互联网 发布:点播软件 编辑:程序博客网 时间:2024/05/31 04:03
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
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
class Solution {public:bool judge(TreeNode* root, int last, int sum){if (0 == root) return false;if ( 0 == root->left && 0 == root->right)return (root->val + last == sum) ? true:false;bool res = judge(root->left, last + root->val, sum);return res? res:judge(root->right, last + root->val, sum);} bool hasPathSum(TreeNode *root, int sum){if (root == 0) return false; return judge(root, 0, sum); }};
0 0
- 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
- Chapter 1 操作系统概述 上(现代操作系统笔记)
- 3月17日--3月23日(共20小时,剩4523小时)
- VITRONICS公司介绍及主要产品
- Android源码开发配置总结
- 今年的GDC真是劲爆啊,虚幻19刀即可得到代码
- Path Sum
- C++笔记:派生类到基类转换到可访问性
- cocos2d-x 模板项目结构分析 3
- iOS 同一设备内的应用之间资源共享的实现
- Oracle数据库常用的4个用户以及win7下登录后消失问题解决
- Android访问本机服务器被拒绝的解决方法
- ARM-v7a 上, backtrace()没有输出
- iOS 绘图时使用 UIGraphicsGetCurrentContext() 报错的原因分析
- Mac 操作系统中使用Finder查看文件完整路径的方法