Create 112. Path Sum.md

This commit is contained in:
唐树森 2018-11-03 14:25:40 +08:00 committed by GitHub
parent dd8fa6310b
commit 22a9600522
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

17
112. Path Sum.md Normal file
View File

@ -0,0 +1,17 @@
# [112. Path Sum](https://leetcode.com/problems/path-sum/description/)
# 思路
根据题意若树空则肯定是false若非空则:
* 若没有左右子树(叶子),判断其值是否满足条件;
* 否则,递归判断其左右子树是否满足,只要有一个满足即满足(即或操作)。
# C++
``` C++
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == NULL) return false;
if(root -> left == NULL && root -> right == NULL && sum == (root -> val)) return true; // 叶子节点
return hasPathSum(root -> left, sum - (root -> val)) || hasPathSum(root -> right, sum - (root -> val));
}
};
```