leetcode: Path Sum
来源:互联网 发布:帝国时代2网络对战 编辑:程序博客网 时间:2024/06/10 00:57
递归,判断如果是叶子且路径和等于目标就返回true
/** * Definition for binary tree * 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 == NULL) return false; return core( root, sum, 0); } bool core( TreeNode *root, int sum, int cur){ if( root->left == NULL && root->right == NULL){ if( root->val + cur == sum) return true; else return false; } else if( root->left == NULL){ return core( root->right, sum, cur+root->val); } else if( root->right == NULL){ return core( root->left, sum, cur+root->val); } else{ return core( root->left, sum, cur+root->val) || core( root->right, sum, cur+root->val); } }};
0 0
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- leetcode:leetcode :Path Sum
- 【Leetcode】Path Sum II (Sum)
- LeetCode: Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- LeetCode Minimum Path Sum
- LeetCode Path Sum
- LeetCode Path Sum II
- [Leetcode] Path Sum
- [Leetcode] Path Sum II
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- 苹果新的编程语言 Swift 语言进阶(十)--类的继承
- 微软新动作 推出智能家居孵化器
- JAVA序列化和反序列化XML
- UIWebView与JavaScript的那些事儿
- Maven学习:常用mvn命令
- leetcode: Path Sum
- opencv提取surf特征点出现的错误
- 明朝后期,翰林学士公鼐执教东宫,时任礼部侍郎
- 406错误spring mvc 返回json格式
- MFC修改全局字体
- Android 代码混淆
- Oracle的select语句用start with...connect by prior子句实现递归查询
- SSH下mysql中文乱码问题
- 使用Def文件导出Dll文件