当前位置:网站首页>Niuke-top101-bm29

Niuke-top101-bm29

2022-06-24 10:29:00 A fish that eats cats

The core idea : recursive

import java.util.*;
public class Solution {
    
    public boolean hasPathSum (TreeNode root, int sum) {
    
        // Empty node cannot find path 
        if(root == null) 
            return false;
        // Leaf node , And the path and are sum
        if(root.left == null && root.right == null && sum - root.val == 0) 
            return true;
        // Recursion into child nodes 
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); 
    }
}

原网站

版权声明
本文为[A fish that eats cats]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240918137867.html