LeetCode OJ--Path Sum II **
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 **的更多相关文章
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【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] 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 ...
- [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 ...
- 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 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python处理excel总结
工作中,大家经常会使用excel去处理数据以及展示,但是对于部分工作我们可以借助程序帮忙实现,达到高效解决问题的效果,比如将接口返回的json解析并保存结果到excel中,按一定规律处理excel中的 ...
- 拖拽图片到另一个div里
HTML代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- Python基础——集合(set)
集合可以去除掉列表中重复的元素. 创建 list1=[123,123,456,789] list1=set(list1) list1 set1=set() type(set1) set1=set([1 ...
- LeetCode(289)Game of Life
题目 According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cel ...
- LeetCode(287)Find the Duplicate Number
题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...
- oracle结构-内存结构与动态内存管理
内存结构与动态内存管理 内存是影响数据库性能的重要因素. oracle8i使用静态内存管理,即,SGA内是预先在参数中配置好的,数据库启动时就按这些配置来进行内在分配,oracle10g引入了动态内存 ...
- day01_07.逻辑与字符串运算符
&&(并且)====>发现&符号总是打错,记忆口令:&7(暗器),在数字7上面,在python中是and ||(或者)====>在python中是or . ...
- GCC内嵌汇编一些限制字符串
/******************/ “b”将输入变量放入ebx “c”将输入变量放入ecx “d”将输入变量放入edx “s”将输入变量放入esi “d”将输入变量放入edi “q”将输入变量放 ...
- Python hash、xml、configparser、sheve、shutil模块讲解 以及 面向对象初识
今日内容: 1.hash模块2.xml模块3.configparser模块4.sheve 模块5.shutil模块 知识点一:hash什么是hash: hash是一种算法,该算法接受传入的的内容,经过 ...
- Thanks for your encourage!
将近三个月的学习,我的努力换回了代表荣誉的小黄衫,这令我很开心啊...我想是不是要写点什么来表达自己的心情呢=,= 于是就有了以下文字ahhhhhh... 学习心得: (1)学习中总会有失败和成功, ...