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解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
- 二叉树单色路径最长&&穿珠子
对树的操作,特别理解递归的好处. //对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的 //另一点结束而形成的路径,而路 ...
随机推荐
- 一、PHP基础-安装PHP集成环境
目录 PHP运行集成环境下载 phpStudy集成环境安装 XAMPP集成环境安装 WampServer环境安装 作者:吴耀田 个人博客:http://www.cnblogs.com/isaacwy ...
- YII2集成GOAOP,实现面向方面编程!
引言: 软件开发的目标是要对世界的部分元素或者信息流建立模型,实现软件系统的工程需要将系统分解成可以创建和管理的模块.于是出现了以系统模块化特性的面向对象程序设计技术.模块化的面向对象编程极度地提高了 ...
- day 26 网络知识 01
一. c/s 架构: 客户端(client)/服务端(server)架构 服务端: 提供服务的 客户端: 享受服务的 B/S 架构:浏览器(browser)/服务端 ...
- 详解PreparedStatement
详解PreparedStatement /** * PrepareStatement 测试插入数据库 */ /** * 如果使用Statement,那么就必须在SQL语句中,实际地去嵌入值,比如之前的 ...
- Mac进度条卡在100%
- 网站用户行为分析——在Ubuntu下安装MySQL及其常用操作
安装MySQL 使用以下命令即可进行mysql安装,注意安装前先更新一下软件源以获得最新版本: sudo apt-get update #更新软件源 sudo apt-get install mysq ...
- QtChart 初体验
早就知道 Qt 5.7 中引入了 QtChart 模块.一直没时间试用.周末正好空闲,就简单的试了试 QtChart.QtChart 学起来还是挺简单的,基于 Qt Graphics View Fra ...
- 1030: [JSOI2007]文本生成器
1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...
- 基于套接字通信的简单练习(FTP)
本项目基于c/s架构开发(采用套接字通信,使用TCP协议) FTP-Socket"""__author:rianley cheng""" 功 ...
- 封装一个Automapper单例
public class DataModule : IModule { public void Configure(IMapperConfigurationExpression cfg) { //cf ...
