leetcode-112-路径总和

/**

 * 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) {

        bool res = false;

        if (root){

            if (root->left == NULL && root->right == NULL) if (root->val == sum) return true;

            res = hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);

        }

        return res;

    }

};

发布了82 篇原创文章 · 获赞 0 · 访问量 1366

猜你喜欢

转载自blog.csdn.net/ChenD17/article/details/104272780
今日推荐