LeetCode--path sum ii
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] ]
问题描述:给出一个二叉树和整数sum,求和等于sum的所有路径列表。
问题解决:根据path sum问题,对每一个节点,除了维护一个nodes, vals LinkedList聊表外,再加一个路径列表,当和为sum时,将这个节点对应的路径加入结果列表中,当所有路径遍历完毕时,返回结果列表。
解法一:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/ public class Solution {public List<List<Integer>> pathSum(TreeNode root, int sum) {LinkedList<TreeNode> nodes = new LinkedList<TreeNode>();LinkedList<Integer> vals = new LinkedList<Integer>();LinkedList<List<Integer>> lc = new LinkedList<List<Integer>>(); //每个节点保存的路径列表 List<List<Integer>> l = new ArrayList<List<Integer>>();//保存最终结果List<Integer> temp = new ArrayList<Integer>(); if(root==null)return null;nodes.add(root);vals.add(root.val);temp.add(root.val);lc.add(temp);while(!nodes.isEmpty()){TreeNode cur = nodes.poll();int num = vals.poll();temp = lc.poll();if((cur.left==null && cur.right==null) && num==sum){l.add(temp);}if(cur.left!=null){nodes.add(cur.left);vals.add(num+cur.left.val);temp.add(cur.left.val);lc.add(temp);}if(cur.right!=null){nodes.add(cur.right);vals.add(num+cur.right.val);temp.add(cur.right.val);lc.add(temp);}}return l;} }
但是这种方法空间。时间开销过大,需要一种新的方法改进。
解法二:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/ public class Solution {public List<List<Integer>> pathSum(TreeNode root, int sum) {List<List<Integer>> res = new ArrayList<List<Integer>>();//保存最终结果List<Integer> curli = new ArrayList<Integer>();int curVal = 0;getPath(root, sum, curVal, res, curli);return res;}public void getPath(TreeNode root, int sum, int curVal,List<List<Integer>> res, List<Integer> curli) {// TODO Auto-generated method stubif(root==null)return;curVal += root.val;curli.add(root.val);if((root.left==null && root.right==null) && curVal==sum){res.add(new ArrayList(curli));return;}if(root.left!=null){getPath(root.left, sum, curVal, res, curli);curli.remove(curli.size()-1);}if(root.right!=null){getPath(root.right, sum, curVal, res, curli);curli.remove(curli.size()-1);}} }
转载于:https://www.cnblogs.com/little-YTMM/p/4531115.html
LeetCode--path sum ii相关推荐
- LeetCode Path Sum II(dfs或者bfs)
问题:给出一个树和一个数,求出从根结点到叶子结点路径和等于这个数的所有情况 思路: 1.深度优先搜索,在到达一个深度结点时,判断是否是叶子结点,并且判断和是否等于要求的数.如果满足,说明是满足条件的一 ...
- [Leetcode] 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 ...
- functionclass[LeetCode]Path Sum II
在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获.只有追求 ...
- LeetCode: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 ...
- LeetCode | 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 ...
- [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 giv ...
- leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)
题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- LeetCode 113. Path Sum II
113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum eq ...
- 113. Path Sum II
/** 113. Path Sum II * 11.18 By Mingyang* 典型的backtracking,不过注意,这里的值可能是负数,所以不能用sum小于0来做任何判断* 1.长度标准:无 ...
最新文章
- 秦州:西瓜书 + 南瓜书 吃瓜系列 13. 降维与度量学习(上)
- react中高阶组件
- linux/hpux 添加用户
- Android 单元测试
- 【题解】Luogu P5279 [ZJOI2019]麻将
- MP3技术白皮书-音频格式全解析
- 【笔记】通过 js——实现 各种跨域
- 我月薪3W,却不用熬夜加班做报表,这个养老工具,你一定要知道
- 多款 D-Link 路由器受多个 RCE 漏洞影响
- CentOS 7部署rsync备份服务器
- Selenium2+python自动化8-SeleniumBuilder辅助定位元素
- Hive 内部表外部表
- Paip.声明式编程以及DSL 总结
- 错误的robots文件设置对SEO的影响
- 加班到凌晨三点?一张图看懂华为员工睡眠时间!!
- TCP三次握手详解-深入浅出(有图实例演示)
- 研发和测试在多国陆续展开 全球央行数字货币研发驶入快车道
- 第3-5课:24 点计算器
- dell计算机在桌面不显示,你好,在吗?我的戴尔笔记本电脑桌面图标不显示为什么?...
- JVM监控和诊断的工具(MAT)
热门文章
- java int a=b指向_java里int a=3,给a赋值的时候,是给它3的地址,还是直接赋值二进制3?...
- python 计算循环次数,05.Python循环
- 手把手教你如何扩展GridView之个性分页
- 授权普通非DBA用户可以有权限查看执行计划的方法
- 干货 | 豆子科技首席架构师钟声:Java的纯真年代
- EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
- Android Animation学习(四) ApiDemos解析:多属性动画
- ubuntu10.04 NFS服务
- 前端工程化开发之yeoman、bower、grunt
- 10个最棒的jQuery视频插件