Algorithms—112.Path Sum

来源:互联网 发布:抓取股票数据 网站 编辑:程序博客网 时间:2024/06/06 09:04

思路:easy的题目,不用动态规划也过了

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean hasPathSum(TreeNode root, int sum) {    if (root==null) {    return false;}    boolean flag1=false;    if (root.val==sum&&root.left==null&&root.right==null) {return true;}    if (root.left!=null) {flag1=hasPathSum(root.left, sum-root.val);}    if (flag1) {return true;}    if (root.right!=null) {    return hasPathSum(root.right, sum-root.val);}    return false;    }}


耗时:352ms

0 0
原创粉丝点击