博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Path Sum II
阅读量:4617 次
发布时间:2019-06-09

本文共 1233 字,大约阅读时间需要 4 分钟。

Description:

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]]

Code:

1 void pathSum(TreeNode* root, int sum, vector
&path,vector< vector
>&result) 2 { 3 path.push_back(root->val); 4 if (root->left == NULL && root->right == NULL && root->val == sum) 5 result.push_back(path); 6 if (root->left!=NULL) 7 pathSum(root->left, sum-root->val, path,result); 8 if (root->right!=NULL) 9 pathSum(root->right, sum-root->val, path,result);10 path.pop_back(); 11 }12 vector
> pathSum(TreeNode* root, int sum) {13 vector< vector
>result;14 if (root==NULL)15 return result; 16 vector
path;17 pathSum(root,sum,path,result);18 return result;19 }

 

转载于:https://www.cnblogs.com/happygirl-zjj/p/4620993.html

你可能感兴趣的文章
JS线程Web worker
查看>>
Flex的动画效果与变换!(三)(完)
查看>>
mysql常见错误码
查看>>
Openresty 与 Tengine
查看>>
使用XV-11激光雷达做hector_slam
查看>>
布局技巧4:使用ViewStub
查看>>
ddt Ui 案例2
查看>>
拿下主机后内网的信息收集
查看>>
LeetCode 876. Middle of the Linked List
查看>>
作业一
查看>>
joj1023
查看>>
动画原理——旋转
查看>>
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>
为什么 NSLog 不支持 Swift 对象(转)
查看>>
Ubuntu 下搭建SVN服务器
查看>>
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>