112.Path Sum
来源:互联网 发布:seo的站外优化流程 编辑:程序博客网 时间:2024/05/23 12:04
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.
对树型结构还是不熟,参考了答案
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left && !root->right) { if(root->val == sum) return true; else return false; } return (hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val)); }};
0 0
- 112. Path Sum && 113. Path Sum II
- 112. Path Sum&113. Path Sum II
- 112.Path Sum
- LeetCode 112. Path Sum
- [LeetCode]112.Path Sum
- 【LeetCode】112.Path Sum
- [Leetcode] 112. Path Sum
- [leetcode] 112.Path Sum
- 112.Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum LeetCode
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- [Unity3D插件]FingerGesture的简单实用
- C#泛型
- Hosts文件浅析
- Tomcat的缺省是多少,怎么修改
- Java assert关键字
- 112.Path Sum
- 无废话WCF入门教程三[WCF的宿主]
- 井字棋代码
- 【4】单机游戏序列化与反序列化
- hdu4734(数位DP)
- Fuse文件系统优化方案
- TCP 与 UDP的区别 java
- 新的一天新的开始
- 15_学生信息的存储与排序