LintCode-376.二叉树的路径和
二叉树的路径和
给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。
一个有效的路径,指的是从根节点到叶节点的路径。样例
给定一个二叉树,和 目标值 = 5:
返回:
[
[1, 2, 2],
[1, 4]
]标签
二叉树 二叉树遍历
code
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
vector<vector<int> > binaryTreePathSum(TreeNode *root, int target) {
// Write your code here
vector<vector<int> > result;
vector<int> order;
TreeNode *p=root;
int path=0;
if(root == NULL) {
return result;
}
else {
path = 0;
findPath(root, target, order, path, result);
return result;
}
}
void findPath(TreeNode *root, int target, vector<int> &order, int &path, vector<vector<int> > &result) {
path += root->val;
order.push_back(root->val);
if(path == target && (root->left==NULL&&root->right==NULL)) {
result.push_back(order);
}
if(root->left != NULL)
findPath(root->left, target, order, path, result);
if(root->right != NULL)
findPath(root->right, target, order, path, result);
path -= root->val;
order.pop_back();
}
};
LintCode-376.二叉树的路径和的更多相关文章
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [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 ...
- lintcode:二叉树的路径和
题目 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 解题 下面有个小bug 最后比较的时候是叶子节点为空,左右都有叶子结点,所 ...
- [LeetCode] 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 Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
随机推荐
- mongodb查看数据库和表的信息
mongodb查看数据库和表的方法比较简单,在为这里推荐使用stats的方法,直观并且详细. 1.查看数据库 db.stats();1输出: { "db" : "siri ...
- php的基础知识(四)
14.数组: 索引数组: 下标就是数字开始的. $arr = ['a','b','c',1,2,3]; 关联数组: $arr = [ 'a' => 'b', 'c' => 'd'; 'e' ...
- array of TVarRec 动态数组使用
FDQuery.AppendRecord()里是一个array of TVarRec.我们一般都是直接用[Var1,Var2,...].这样手工输入,但如果增加的元素我们预先不知道,就要声明一个arr ...
- scala 获取当前时间的两种方式
在编写程序时,有时需要获取当前时间,这在记录异常信息.获取程序运行耗时很有用处 方式一: val time1=System.currentTimeMillis() 这种方式获取的是程序运行到此的毫秒数 ...
- python在lxml中使用XPath语法进行#数据解析
在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...
- 20145234黄斐《网络对抗技术》实验一,逆向及Bof基础实践
实践内容 本次实践的对象是一个名为hf20145234的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段, ...
- Android 打印log 在logcat 看不到
今天调试一个问题,因为是插件,只能通过打印log 定位问题. 但是打印了log 一直没有看到. 代码如下: Log.d("","aaaa24"); 后来发现是需 ...
- elasticsearch增删改查操作
目录 1. 插入数据 2. 更改数据 3. 删除数据 4. 检索文档 1. 插入数据 关于下面的代码如何使用,可以借助于kibana的console,浏览器打开地址: http://xxx.xxx.x ...
- 11gR2RAC更换CRS磁盘组文档
磁盘(pv)准备 在生产环境中,提前从存储上划分一些磁盘挂载到RAC系统的两个节点上(node1,node2). 新增加磁盘组为(hdisk14--hdisk24) 1.1磁盘使用规划 ...
- 初识c++模板元编程
模板元编程(Template metaprogramming,简称TMP)是编译器内执行的程序,编译器读入template,编译输出的结果再与其他源码一起经过普通编译过程生成目标文件.通俗来说,普通运 ...
