leetcode——112. 路径总和

class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root: return False
        if not root.left and not root.right and sum - root.val == 0:return True
        return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
执行用时 :48 ms, 在所有 python3 提交中击败了97.76%的用户
内存消耗 :15.6 MB, 在所有 python3 提交中击败了5.06%的用户
 
 
0.。。。。。。
 
——2019.11.17
 

复习;
public boolean 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);
    }

 

 

——2020.7.2

posted @ 2019-11-17 22:24  欣姐姐  阅读(117)  评论(0编辑  收藏  举报