LeetCode 二叉树路径问题 Path SUM(①②③)总结

来源:互联网 发布:python编程求圆的面积 编辑:程序博客网 时间:2024/06/02 06:44


(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/77159954冷血之心的博客)


题目一:Path Sum


题目大意是这个意思,给定一棵二叉树和一个sum值,判断树中是否存在一条从根节点到叶子节点的路径,使得路径上的值加起来刚好等于sum。

解题思路:

递归结束条件:

root == null返回false,表示不存在;

root.left == null && root.right == null && sum - root.val == 0 ;返回true,表示找到了路径

递归过程:

依次从左子树和右子树中查找,注意sum = sum - root.val

/** * 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;        if(root.left == null && root.right == null && sum - root.val == 0)             return true;        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);    }}


题目二:Path Sum 2


题目大意是找到所有满足条件的路径并且返回;

解题思路:

将当前节点root的值放入list中更新sum值,判断当前节点是否满足递归条件root.left == null && root.right == null&&sum == 0;

若满足,则将存有当前路径的list值存入最后的大list中

然后依次递归左子树和右子树

从存有当前路径的list中去除最后一个节点,这样便可以返回到了当前叶子节点的父节点


/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    List<List<Integer>> listAll = new ArrayList<>();    List<Integer> list = new ArrayList<>();    public List<List<Integer>> pathSum(TreeNode root, int sum) {        if(root == null)             return listAll;          list.add(root.val);          sum -= root.val;          if(root.left == null && root.right == null&&sum == 0)              listAll.add(new ArrayList<Integer>(list));          pathSum(root.left, sum);          pathSum(root.right, sum);          list.remove(list.size()-1);         return listAll;      }}


题目三:Path Sum 3


题目大意是说,路径的定义不必从根节点开始,也不必终止与叶子节点,只需要保证从父节点指向子节点即可。

解题思路:

我们定义一个辅助函数 path(TreeNode root,int sum),表示在以node为根节点的二叉树中,寻找包含node的路径,和为sum,返回路径的个数;

该函数的返回值由三部分组成:

1、当前root.val是否等于sum

2、左子树递归

3、右子树递归

由于路径的根节点可以不是从该树的root节点开始,所以还需要递归计算root.left和root.right作为根节点的路径个数

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    // 在以root为根节点的二叉树中,寻找和为sum的路径,返回这样的路径个数       public int pathSum(TreeNode root, int sum) {        if(root==null)            return 0;        return path(root,sum)+pathSum(root.left,sum)+pathSum(root.right,sum);    }    // 在以root为根节点的二叉树中,寻找包含root的路径,和为sum    // 返回路径的个数    private int path(TreeNode root, int sum){        if(root==null)            return 0;        return (root.val == sum?1:0)+path(root.left,sum-root.val)+path(root.right,sum-root.val);    } }


以上是Leetcode上关于求路径的三个常见的算法题,我们需要注意的是:

1、递归的结束条件有哪些?

2、递归过程怎么写?

3、注意特殊条件

在关于二叉树的递归调用中,我们经常需要自己额外写一个辅助函数,用来实现递归,在辅助函数中,我们可以传入一些必要的参数~


如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以进群366533258一起交流学习哦~



本群给大家提供一个学习交流的平台,内设菜鸟Java管理员一枚、精通算法的金牌讲师一枚、Android管理员一枚、蓝牙BlueTooth管理员一枚、Web前端管理一枚以及C#管理一枚。欢迎大家进来交流技术。



阅读全文
2 1
原创粉丝点击