LeetCode OJ: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 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]
]
求路径的和与某一特定的值时候对等即可,简单的dfs,代码如下:
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<int> res;
dfs(root, res, sum);
return ret;
}
void dfs(TreeNode * root, vector<int> res, int left)
{
if(!root) return;
if(!root->left && !root->right && left == root->val){
res.push_back(root->val);
ret.push_back(res);
}
if(left <= root->val)
return;
else{
res.push_back(root->val);
if(root->left)
dfs(root->left, res, left -= root->val);
if(root->right)
dfs(root->right, res, left -= root->val);
}
}
private:
vector<vector<int>> ret;
};
java版本代码如下,方法相同,就是java的引用处理起来稍微麻烦点,递归尾部应该pop一下。
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
Stack<Integer> tmp = new Stack<Integer>();
dfs(ret, tmp, root, sum);
return ret;
}
public void dfs(List<List<Integer>> ret, Stack<Integer> tmp, TreeNode root, int remain){
if(root == null) return;
tmp.push(root.val);
if(root.left == null && root.right == null)
if(remain == root.val)
ret.add((Stack<Integer>)tmp.clone());
if(root.left != null) dfs(ret, tmp, root.left, remain - root.val);
if(root.right != null) dfs(ret, tmp, root.right, remain - root.val);
tmp.pop();
}
}
LeetCode OJ:Path Sum II(路径和II)的更多相关文章
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- [LeetCode] Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- LeetCode 112. Path Sum(路径和是否可为sum)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [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 ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [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 ...
随机推荐
- windows 安装 python _ flask
1:首先安装python虚拟环境;(略) 2: 添加一个虚拟环境: 在你的项目目录里直接 virtualenv venv 启动虚拟环境;\venv\Scripts 直接运行activate 3: 在虚 ...
- 003-整型入栈指令(iconst、bipush、sipush、ldc)
一.概述 官网:http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html参考地址:http://www.linmuxi.com/201 ...
- 视频解码芯片SAA7111A的初始化
为了完成SAA7111A的初始化,FPGA需要完成I2C控制器的设计. 之前本科的时候初步接触过I2C,但是编写一个Verilog的控制器还是觉得有点难度的.说实话,那时候都无从下手,对实现这样一个协 ...
- 进程理论基础(Day32)
背景知识 顾名思义,进程即一个软件正在进行的过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老的也是最重要的抽象概念之一.操作系统的其他 ...
- Ubuntu 下 java 版本的切换
切换的方法很简单,使用下面的两个命令即可: update-alternatives --config java update-alternatives --config javac eg: root@ ...
- 常用模块之hashlib,configparser,logging模块
常用模块二 hashlib模块 hashlib提供了常见的摘要算法,如md5和sha1等等. 那么什么是摘要算法呢?摘要算法又称为哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定 ...
- 深度学习2--安装opencv3.1
1\opencv的安装参考视频 2\ 以下内容来自:http://blog.csdn.net/l18930738887/article/details/54696148 本人因为被坑过,所以建议各位最 ...
- VC引用静态库
对于路径的设置: Tools->Options->Directory中设置的Lib路径,是给VC环境设置的, 所以只要是这个VC打开的项目,都会包含这些路径. Project->Se ...
- Js 类型方面的神坑
你有没有遇见过本来好好的一个数组结果 typeof 出来是个 object 的情况,你有没有遇到过非要写个 typeof x === "undefined" 判断未赋值的情况... ...
- Sublime Text3 打开文档乱码
一.安装包管理器使用Ctrl+~快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码 import urllib.request,os; pf = 'Package C ...