[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]
]
这题其实就是深度搜索,代码:
#include <vector>
#include <iostream>
using namespace std;
/**
* Definition for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<vector<int> > ret;
vector<int> nowPath;
vector<vector<int> > pathSum(TreeNode *root, int sum) {
ret.clear();
nowPath.clear();
if(root==NULL) return ret;
help_f(root,sum);
return ret;
} void help_f(TreeNode *root,int sum)
{
if(root==NULL) return ;
nowPath.push_back(root->val);
help_f(root->left,sum - root->val);
help_f(root->right,sum - root->val);
if(root->left==NULL&&root->right==NULL&&sum==root->val)
ret.push_back(nowPath);
nowPath.pop_back();
}
}; int main()
{
return ;
}
[LeetCode] Path Sum II 深度搜索的更多相关文章
- [leetcode]Path Sum II
		Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ... 
- LeetCode: Path Sum II 解题报告
		Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ... 
- [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]Path Sum II @ Python
		原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ... 
- 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 Path Sum II  (DFS)
		题意: 给一棵二叉树,每个叶子到根的路径之和为sum的,将所有可能的路径装进vector返回. 思路: 节点的值可能为负的.这样子就必须到了叶节点才能判断,而不能中途进行剪枝. /** * Defin ... 
- LeetCode:Path Sum I II
		LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ... 
随机推荐
- pyhon之99乘法表
			1.长方形完整格式 for i in range(1,10): for j in range(1,10): print("%d*%d" %(j,i),end=" &quo ... 
- Install Jenkins 2.1.36 and openjdk 1.7.0 on centos 7
			#!/bin/bash## Copyright (c) 2014-2015 Michael Dichirico (https://github.com/mdichirico)# This softwa ... 
- spring MVC体系结构和请求控制器
			MVC处理过程 spring MVC架构模式都进行了分层设计如下 数据访问接口:DAO层 处理业务逻辑层:service层 数据实体:POJO 负责前端请求的接受并处理:servlet 负责前端页面展 ... 
- 第8课 Thinkphp 5 update判断修改成功与失败 Thinkphp5商城第四季
			没有修改数据时,判断修改成功与失败 如果提交时的数据库里之前的数据一样(即没有修改就提交表单),会返回0,此时 判断修改成功用$save !== false 这样才会提示修改成功. $save=db( ... 
- 使用JFreeChart生成报表
			1.JFreeChart简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计. ... 
- A * B Problem Plus HDU - 1402 (FFT)
			A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ... 
- Cuba studio框架中使用thymeteaf模板时中文乱码
			最近公司换了Cuba这个orm框架,框架中使用了thymeteaf模板技术,发现在html中解析汉字一直是乱码的存在 一直以为是tomcat的问题但是tomcat的server.xml,项目中的web ... 
- 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist
			不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句: ./spark-shell --master spark://ma ... 
- 【Copy List with Random Pointer】cpp
			题目: A linked list is given such that each node contains an additional random pointer which could poi ... 
- Hive jdbc连接出现java.sql.SQLException: enabling autocommit is not supported
			1.代码如下 String url = "jdbc:hive2://master135:10000/default"; String user = "root" ... 
