240
笔下虽有千言,胸中实无一策

30 Day Challenge Day 15 | Leetcode 112. Path Sum

题解

Easy | Tree, DFS

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if(!root) return false;
        if(!root->left && !root->right) return root->val == sum;
        return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);        
    }
};
posted @ 2020-09-30 12:04  CasperWin  阅读(53)  评论(0编辑  收藏  举报