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. proc_info_list

    内核中每种处理器架构抽象为一个proc_info_list结构体,在arch/arm/include/asm/procinfo.h中定义, struct proc_info_list { unsign ...

  2. poj 1017 装箱子问题 贪心算法

    题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来.问:至少需要多少个6*6箱子 思路: 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水. 同样放在本题就是先装6*6然后5*5... ...

  3. ubuntu12.04 ppa安装git

    PPA地址:https://launchpad.net/~git-core/+archive/ppa 支持所有的Ubuntu版本. 运行命令: sudo apt-add-repository ppa: ...

  4. CSS效果小结

    效果属性 1.box-shadow(盒子阴影) 示例 加上 box-shadow 内阴影 复杂例子 阴影的形状跟原来的形状是一样的 结果: box-shadow 作用:1.营造层次感(立体感)2.充当 ...

  5. docker容器utf-8编码问题

    locale -a export LANG=C.UTF-8 locale 在docker容器中python脚本汉字的会乱码 按照上面的方法暂时的设置下容器 编码可以解决脚本中文乱码问题

  6. acdsee 15中文版的许可证密钥+激活方法

    按以下方法就可以使用了,进入注册表的命令是:开始-运行-输入regedit   进入注册表后按下面的步骤操作就行. ACDSee15中文版激活英文版激活码5NR9CW-SSRMMY-KFWMQU-ZP ...

  7. LPSTR LPCSTR LPWSTR LPCWSTR区别

    LPSTR   一个32位的指向字符串的指针    LPCSTR   一个32位的指向字符串常量的指针    LPWSTR   一个32位的指向unicode字符串的指针    LPCWSTR   个 ...

  8. Python学习-day8 socket进阶

    还是继续socket网络编程的学习. socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None) Socket Fami ...

  9. PHP smarty模版引擎基本安装

    环境:  PHP5.2 以上版本 先去官网下载smarty模版引擎的库文件到你的电脑或服务器上 smarty官方网站库文件下载地址: https://www.smarty.net/download 下 ...

  10. 基于 FPGA 的图像边缘检测

    本文主要内容是实现图像的边缘检测功能 目录 mif文件的制作 调用 ip 核生成rom以及在 questasim 仿真注意问题 灰度处理 均值滤波:重点是3*3 像素阵列的生成 sobel边缘检测 图 ...