首页 > 试题广场 >

二叉树中和为某一值的路径(一)

[编程题]二叉树中和为某一值的路径(一)
  • 热度指数:163461 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个二叉树root和一个值 sum ,判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径。
1.该题路径定义为从树的根结点开始往下一直到叶子结点所经过的结点
2.叶子节点是指没有子节点的节点
3.路径只能从父节点到子节点,不能从子节点到父节点
4.总节点数目为n

例如:
给出如下的二叉树,

返回true,因为存在一条路径 的节点值之和为 22

数据范围:
1.树上的节点数满足
2.每 个节点的值都满足
要求:空间复杂度 ,时间复杂度
进阶:空间复杂度 ,时间复杂度
示例1

输入

{5,4,8,1,11,#,9,#,#,2,7},22

输出

true
示例2

输入

{1,2},0

输出

false
示例3

输入

{1,2},3

输出

true
示例4

输入

{},0

输出

false

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
开始以为是只要到sum就满足,后来发现是到最子节点才行
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        //if(sum==0) return false;
        if(root==null) return false;
        boolean[] res = new boolean[] {false};
        return dfs(root, sum, res)[0];
    }

    private boolean[] dfs(TreeNode root, int sum, boolean[] res) {
        // if (sum == 0) {
        //     res[0] = true;
        //     return res;
        // }

        if (root.left != null && root.right != null) {
            dfs(root.right, sum - root.val, res);
            dfs(root.left, sum - root.val, res);
        }

        if (root.left == null && root.right == null) {
            sum -= root.val;
            if (sum == 0) {
                res[0] = true;
                return res;
            }
        } else if (root.left == null && root.right != null) {
            dfs(root.right, sum - root.val, res);
        } else if (root.right == null && root.left != null) {
            dfs(root.left, sum - root.val, res);
        }

        return res;
    }

    
}

发表于 2023-10-16 20:25:49 回复(0)
public class Solution {
    /**
     * 给定一个二叉树root和一个值 sum ,判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径
     *
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        if (null == root) {
            return false;
        }
        if (sum - root.val == 0 && isLeafNode(root)) {
            return true;
        }
        return  hasPathSum(root.left, sum - root.val) ||
                hasPathSum(root.right, sum - root.val);
    }

    /**
     * 判断节点是不是叶子节点
     *
     *
     * @param node TreeNode类
     * @return bool布尔型
     */
    public boolean isLeafNode(TreeNode node) {
        return null == node.left && null == node.right;
    }

}

发表于 2023-10-12 17:15:50 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        if(root==null) return false;
        if(sum-root.val==0&&root.left==null&&root.right==null) return true;

        return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
    }
}

发表于 2023-09-11 17:28:54 回复(0)
public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if (root == null) {
            return false;
        }
        return process(root, sum);
    }

    private boolean process(TreeNode root, int target) {
        if (root.left == null && root.right == null) {
            return root.val == target;
        }
        boolean left = false;
        boolean right = false;
        if (root.left != null) {
            left = process(root.left, target - root.val);
        }
        if(root.right != null) {
            right = process(root.right, target - root.val);
        }
        return left | right;
    }

发表于 2023-08-26 14:55:43 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    // class Sum {
    //     TreeNode node = null;
    //     int sum = 0;
    //     Sum(TreeNode node, int sum) {
    //         this.sum = sum;
    //         this.node = node;
    //     }
    // }
    // public boolean hasPathSum (TreeNode root, int sum) {
    //     // write code here
    //     Queue<Sum> que = new ArrayDeque<>();
    //     if (root == null) return false;
    //     Sum temp = new Sum(root, root.val);
    //     que.add(temp);
    //     while (!que.isEmpty()) {
    //         Sum t = que.poll();
    //         if (t.node.left != null) {
    //             que.add(new Sum(t.node.left, t.sum + t.node.left.val));
    //         }
    //         if (t.node.right != null) {
    //             que.add(new Sum(t.node.right, t.sum + t.node.right.val));
    //         }
    //         if(t.node.right==null&&t.node.left==null&&t.sum==sum){
    //             return true;
    //         }
    //     }
    //     return false;
    // }
    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);
    }

}

发表于 2023-07-24 19:56:51 回复(0)
dfs+回溯

发表于 2023-07-17 16:30:10 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    List<List<Integer>> ans=new LinkedList<>();
    LinkedList<Integer> path=new LinkedList<>(); 
    public boolean hasPathSum (TreeNode root, int sum) {
        //回溯,统计叶子节点的路径和
        boolean ansss=false;
        build(root,sum);
        if (ans.isEmpty()==true){
            return false;
        }else {
            return true;
        }
         
    }
    public void build(TreeNode root,int sum){
        if(root==null){
            return ;
        }
        path.add(root.val);
        sum=sum-root.val;
        if(sum==0&&root.left==null&&root.right==null){
            ans.add(new LinkedList<>(path));
        }
        build(root.left,sum);
        build(root.right,sum);
        path.removeLast();
    }
    
}

发表于 2023-05-29 21:03:40 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if(root==null)return false;
        sum-=root.val;
        if(root.left==null && root.right==null && sum==0)return true;
        return hasPathSum(root.left,sum) || hasPathSum(root.right,sum);
    }
}
发表于 2023-03-27 22:01:14 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if (root == null) {
            return false;
        }
        
        return isHasPathSum(root,sum,0);

    }

    private boolean isHasPathSum(TreeNode root, int sum, int temp) {
        if (root == null) {
            return false;
        }
        if(root.left == null && root.right == null) {
            return sum == root.val+temp;
        }
        boolean r1 = isHasPathSum(root.left, sum, root.val + temp);
        if (r1) {
            return r1;
        }
        boolean r2 = isHasPathSum(root.right, sum, root.val + temp);
        return r2;
    }



}

发表于 2022-11-05 14:43:38 回复(0)
递归查找 dfs
public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
         // 等于null返回false
        if(root == null) 
            return false;
        
        // 如果到了最后一个节点,并且当前val = sum  才返回true
        if(root.left==null && root.right == null && root.val==sum)
            return true;
        
        // 左递归,右递归,  sum每次都递减当前的值
        return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right,sum-root.val);
    }

}


发表于 2022-11-03 21:29:45 回复(2)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        return dfs(root,sum);
    }
    boolean dfs(TreeNode root, int num) {
        if (root == null)
            return false;
        num -= root.val;
        if (root.left == null && root.right == null && num == 0)
            return true;
        return dfs(root.left,num) || dfs(root.right,num);
        }
}

发表于 2022-10-10 21:38:00 回复(0)
public boolean hasPathSum (TreeNode root, int sum) {
    // 递归终止条件
    if (root == null) return false;
    if (root.left == null && root.right == null && root.val == sum) return true;
    // 判断左子树的路径和=sum-root.val
    // 判断右子树的路径和=sum-root.val
    return hasPathSum(root.left, sum - root.val)
        || hasPathSum(root.right, sum - root.val);
}

发表于 2022-09-19 01:47:26 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if(root==null) return false;
        List<Integer> list = new ArrayList();
        visit(root,0,list);
        return list.contains(new Integer(sum));
    }
    
    
    private void visit(TreeNode root,int count,List<Integer> list){
        count = count + root.val;
        if(root.left==null&&root.right==null) list.add(count);
        if(root.left!=null)visit(root.left,count,list);
        if(root.right!=null)visit(root.right,count,list);
        
    }
}

发表于 2022-09-06 11:59:57 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    boolean tag = false;
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        bfs(root, 0, sum);
        return tag;
    }

    public void bfs(TreeNode root, int count, int target) {
        if (root == null) return;
        if (root.left == null && root.right == null && root.val + count == target) {
            tag = true;
            return;
        }

        bfs(root.left, count + root.val, target);
        bfs(root.right, count + root.val, target);
    }
}

发表于 2022-08-26 10:13:41 回复(0)
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @param sum int整型 
     * @return bool布尔型
     */
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if(root == null){
            return false;
        }
        
        sum-= root.val;
        
        if(sum == 0 && root.left == null && root.right == null){
            return true;
        }
        
        return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
    }
}

发表于 2022-08-17 14:02:41 回复(0)
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        return midtraversel(root,sum);
    }
    
     public boolean midtraversel(TreeNode root, int sum){
         if (root == null) return false;
         if (root.left == null && root.right == null) {
            if (sum-root.val != 0 ) return false;
            return true;
        }
        return  midtraversel(root.left,sum - root.val) || midtraversel(root.right,sum-root.val);
    }
}

发表于 2022-06-21 11:04:56 回复(0)
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if(root==null){
            return false;
        }
        sum-=root.val;
        if(root.left==null && root.right==null && sum==0){
            return true;
        }
        return hasPathSum(root.left ,sum) || hasPathSum(root.right ,sum);
    }

发表于 2022-06-19 21:20:13 回复(0)
import java.util.*;
public class Solution {
    //深度遍历路径中节点值叠加
    static int add = 0;
    //root到当前节点总共的叠加次数
    static int count = 0;
    static boolean flag = false;
    static int temp = 0;
    public void dfs(TreeNode node, int sum) {
        if (node == null ) {
            if (add == sum && count > 1) {
                flag = true;
            }
            return;
        }
        add += node.val;
        count++;
        temp = node.val;
        dfs(node.left, sum);
        dfs(node.right, sum);
        //回溯
        add -= node.val;
        count--;
    }
    public boolean hasPathSum (TreeNode root, int sum) {
        // 特殊情况判断
        if (root == null) return false;
        if (root.left == null && root.right == null) {
            if (root.val == sum) {
                return true;
            } else {
                return false;
            }
        }
        //进入dfs递归
        dfs(root, sum);
        return flag;
    }
}

发表于 2022-06-15 10:19:01 回复(0)

问题信息

难度:
84条回答 28895浏览

热门推荐

通过挑战的用户