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.长度标准:无 ...
最新文章
- elasticsearch按照配置时遇到的一些坑 [Failed to load settings from [elasticsearch.yml]]
- linux执行python命令后permission denied
- OpenVINO 2019 R2.0 Custom Layer Implementation for linux(2)
- ROS实现两台计算机之间的网络通信
- Kubernetes详解(十五)——Pod对象创建过程
- 概率论综述(题型篇)
- 重庆北大青鸟校区【学员心声】:学习是我们前进的动力
- java add方法怎么用_Java ArrayList add() 方法
- 如何搭建一个完整的视频直播系统?
- vue3.0父子组件警告Extraneous non-emits event listeners (closeSetDialog) were passed to component but could
- 用JSP-Servlet构建三层式管理信息系统
- 第4期-通过起点中文网爬取小说
- Diagnostics - DID, DTC区别与联系
- FreeMarker 网页静态化
- 阿里云ESC云盘挂载
- 51单片机Proteus仿真+Keil工程-实验2-按键-数码管
- 04DeepLab-01论文背景、研究成果及意义
- mips汇编代码示例解释_通过示例解释cosmosdb
- html中判断数组是否为空,jquery如何判断数组是否为空?
- Pytorch的Dataset和DataLoader