【easy】112.path sum 113.-----------------

求是否有从根到叶的路径,节点和等于某个值。

/**
 * 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 == NULL) {
            return false;
        }
        if (root->left == NULL && root->right == NULL) {
            return sum == root->val;
        }
        return hasPathSum (root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
    }
};

返回所有和为特定值的路径

return

[
   [5,4,11,2],
   [5,8,4,5]
]
未完待续
posted @ 2018-01-22 21:17  Sherry_Yang  阅读(95)  评论(0编辑  收藏  举报