path-sum-ii
来源:互联网 发布:淘宝买的手机如何保修 编辑:程序博客网 时间:2024/06/14 15:21
题目:
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 andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
程序:
class Solution {public: vector<vector<int> > pathSum(TreeNode *root, int sum) { vector<vector<int>> res; if(root==NULL) return res; solve(res,root,sum); return res; } void solve(vector<vector<int>> &res,TreeNode *root,int sum) { if(root&&root->left==NULL&&root->right==NULL&&sum==root->val) { v.push_back(root->val); res.push_back(v); v.pop_back(); return; } if(root==NULL) return; v.push_back(root->val); solve(res,root->left,sum-root->val); solve(res,root->right,sum-root->val); v.pop_back(); } vector<int> v;};
阅读全文
0 0
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- [113]Path Sum II
- Path Sum II
- Path Sum II
- [LeetCode]Path Sum II
- Path Sum II
- Path Sum II
- [Leetcode]Path Sum II
- [leetcode]Path Sum II
- Path Sum II
- Leetcode: Path Sum II
- Path Sum II
- Qt学习笔记——事件处理
- git 命令整合
- 前言
- 欧几里得空间
- java提高篇-----抽象类与接口
- path-sum-ii
- 将JSON对象、数组的值赋给另外一个对象,(互不影响) 对象赋值
- GIT相关整理
- Java中的两种异常类型是什么?他们有什么区别?
- TCP协议
- 基于rk3288平台android5.1系统的wifi流程分析 ---- 连接热点
- Java全局变量与局部变量的整理
- 真正从零开始,TensorFlow详细安装入门图文教程
- Java线程工作内存与主内存变量交换过程及volatile关键字理解