https://oj.leetcode.com/problems/path-sum-ii/

树的深搜,从根到叶子,并记录符合条件的路径。

注意参数的传递,是否需要使用引用。

#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<vector<int> > ans;
vector<vector<int> > pathSum(TreeNode *root, int sum)
{
ans.clear();
if(root == NULL)
return ans;
vector<int> ansPiece;
hasPathSum(root,sum,ansPiece);
return ans;
}
void hasPathSum(TreeNode *root, int sum, vector<int> ansPiece) {
//null
if(root == NULL )
return ; //leaf node
if(root->left == NULL && root->right == NULL && root->val == sum)
{
ansPiece.push_back(root->val);
vector<int> _ansPiece = ansPiece;
ans.push_back(_ansPiece);
return ;
}
if(root->left == NULL && root->right == NULL)
return ; //not leaf node
if(root->left ||root->right)
{
ansPiece.push_back(root->val); if(root->left)
hasPathSum(root->left, sum - root->val,ansPiece); if(root->right)
hasPathSum(root->right, sum-root->val, ansPiece);
}
return;
}
}; int main()
{
TreeNode *n1 = new TreeNode();
TreeNode *n2 = new TreeNode(-);
TreeNode *n3 = new TreeNode(-);
TreeNode *n4 = new TreeNode();
TreeNode *n5 = new TreeNode();
TreeNode *n6 = new TreeNode(-);
TreeNode *n7 = new TreeNode();
TreeNode *n8 = new TreeNode();
n1->left = n2;
n1->right = n3;
n2->left = n4;
n2->right = n5;
n3->left = n6;
n3->right = n8;
n4->left = n7;
class Solution myS;
myS.pathSum(n1,);
return ;
}

LeetCode OJ--Path Sum II **的更多相关文章

  1. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  2. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  3. 【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 ...

  4. [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 ...

  5. [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 given su ...

  6. 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 given su ...

  7. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  8. LeetCode 113. Path Sum II路径总和 II (C++)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  9. 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 given su ...

  10. leetcode 113 Path Sum II ----- java

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. 每天一个linux命令(13):less命令

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  2. Docker 容器的网络连接 & 容器互联

    1. Docker 容器网络基础架构 Docker0 ifconfig查看到的 docker0 是linux的虚拟网桥(OSI数据链路层) docker0 地址划分: 172.17.42.1 255. ...

  3. Linux菜鸟起飞之路【八】文本编辑器

    在Linux中,文本编辑器有两个,VI和VIM.这两个编辑器用法差不多,但vim是vi的升级版,所以功能更强大一些. vim编辑器一共有三种模式,命令行模式.编辑模式和扩展模式. 进入vim界面,首先 ...

  4. DNS预解析 dns-prefetch

    1.DNS 是什么? Domain Name System,域名系统,作为域名和IP地址相互映射的一个分布式数据库. DNS大家都懂,那么浏览器访问域名的时候,是需要去解析一次DNS,也就是把域名 g ...

  5. matplotlib 设置图形大小时 figsize 与 dpi 的关系

    matplotlib 中设置图形大小的语句如下: fig = plt.figure(figsize=(a, b), dpi=dpi) 其中: figsize 设置图形的大小,a 为图形的宽, b 为图 ...

  6. centos配置jdk

    ########## config jdk ########## export JAVA_HOME=/usr/local/java/jdk1.7.0_79 export CLASSPATH=.:${J ...

  7. bash实例

    1写一个脚本,完成如下功能(使用函数):1.脚本使用格式:mkscript.sh [-D|--description "script description"] [-A|--aut ...

  8. dubbo基础文档

    随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个应用, ...

  9. Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  10. 九度oj 题目1041:Simple Sorting

    题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill p ...