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

Path Sum II leetcode java

描述

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
]

解析

除了要判断是否有这样的一个path sum,还需要把所有的都可能性结果都返回,所以就用传统的DFS递归解决子问题。

将当前节点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; }
* }
*/
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;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void pathSumHelper(TreeNode root, int sum, List<Integer> sumlist, List<List<Integer>> pathlist) {
if (root == null)
return;
sumlist.add(root.val);
sum = sum - root.val;
if (root.left == null && root.right == null) {
if (sum == 0) {
pathlist.add(new ArrayList<Integer>(sumlist));
}
} else {
if (root.left != null)
pathSumHelper(root.left, sum, sumlist, pathlist);
if (root.right != null)
pathSumHelper(root.right, sum, sumlist, pathlist);
}
sumlist.remove(sumlist.size() - 1);
} public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> pathlist = new ArrayList<List<Integer>>();
List<Integer> sumlist = new ArrayList<Integer>();
pathSumHelper(root, sum, sumlist, pathlist);
return pathlist;
}
}

[LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)的更多相关文章

  1. [LeetCode] 113. Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  2. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  3. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  5. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  7. [leetcode]113. Path Sum II路径和(返回路径)

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. leetcode 113 path Sum II 路径和

    递归先序遍历+vector<int>容器记录路径 /** * Definition for a binary tree node. * struct TreeNode { * int va ...

  9. Leetcode 113. Path Sum II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. git服务器搭建全程

    为了后续安装能正常进行,我们先来安装一些相关依赖库和编译工具 [root@VM_95_113_centos ~]# yum install curl-devel expat-devel gettext ...

  2. SVN服务器搭建和使用教程

    安装SVNserver 点击Next下一步,如下: 然后再点击Next项,下一步,如下: 点击[Next] 如下: Location是指VisualSVN Server的安装目录,Repository ...

  3. 关于python的“重载”

    首先,关于python和java的区别: 1.Java有是通过方法名和方法列表来定义一个函数,python是通过方法名来定义一个函数(不允许方法名相同的函数存在) 2.java是通过定义多个相同方法名 ...

  4. java 之2D过气游戏类的写法

    2D游戏中各对象的父类 package cn.littlepage.game; import java.awt.Graphics; import java.awt.Image; import java ...

  5. 《剑指offer》第五十五题(平衡二叉树)

    // 面试题55(二):平衡二叉树 // 题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉树中 // 任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树. #includ ...

  6. 了解java中垃圾回收机制

    Java的垃圾回收机制是Java环境自带有的,它不像c语言的malloc申请空间后需要Free()函数来释放,而Java中的代码块中所申请的空间可在程序执行完成后自动释放,但是是有局限性的,代码块所占 ...

  7. python读写json文件(转)

    https://www.cnblogs.com/bigberg/p/6430095.html 利用python中的json库处理数据(包含json的四种方法:dumps.dump.loads.load ...

  8. c# 读取txt方法

    string strLine; try { FileStream aFile = new FileStream("Log.txt", FileMode.Open); StreamR ...

  9. ubuntu 14.04 使用 xfce4 的时候,会有图标问题

    有些程序的图标丢失了,结果十分影响整体用户体验. 解决方法: sudo apt-get install gnome-icon-theme-full tango-icon-theme 然后重新进入就行了 ...

  10. JavaScript动态加载资源

    //动态加载样式 function dynamicLoadingCss(path){ if(!path || path.length === 0){ return false; } var head ...