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

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

Path Sum I 

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:

Given the below binary tree and sum = 22,

5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

递归求解就可以。注意叶子结点条件是左右结点都为空。

1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     bool hasPathSum(TreeNode *root, int sum) {13         if (root == NULL) return false;14         return recursive(root, sum, 0);15     }16     17     bool recursive(TreeNode *root, int sum, int cur) {18         if (root == NULL) {19             return false;20         }21         22         cur += root->val;23         24         // leaf25         if (root->left == NULL && root->right == NULL) {26             return cur == sum;27         }28         29         bool res = recursive(root->left, sum, cur);30         if (res) return true;31         return recursive(root->right, sum, cur);32     }33 };

第三次刷写得简洁许多,我今天已经重复了好多次这一句了。。。

1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     bool hasPathSum(TreeNode *root, int sum) {13         if (root == NULL) return false;14         sum -= root->val;15         if (root->left == NULL && root->right == NULL && sum == 0) return true;16         return (hasPathSum(root->left, sum) || hasPathSum(root->right, sum));17     }18 };

Path Sum II

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]]
1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     vector
> pathSum(TreeNode *root, int sum) {13 if (root == NULL) return ret;14 vector
v;15 16 int curSum = 0;17 recursive(root, sum, curSum, v);18 return ret;19 }20 21 void recursive(TreeNode *root, int sum, int curSum, vector
&v) {22 if (root == NULL) return;23 24 curSum += root->val;25 v.push_back(root->val);26 if (root->left == NULL && root->right == NULL) {27 if (curSum == sum) {28 ret.push_back(v);29 }30 }31 recursive(root->left, sum, curSum, v);32 recursive(root->right, sum, curSum, v);33 v.pop_back();34 }35 36 private:37 vector
> ret;38 };
第三次。
1 class Solution { 2 public: 3     void recurse(TreeNode *root, int sum, vector
&temp, vector
> &ans) { 4 if (root == NULL) return; 5 sum -= root->val; 6 temp.push_back(root->val); 7 if (root->left == NULL && root->right == NULL && sum == 0) { 8 ans.push_back(temp); 9 } else {10 recurse(root->left, sum, temp, ans);11 recurse(root->right, sum, temp, ans);12 }13 temp.pop_back();14 }15 vector
> pathSum(TreeNode *root, int sum) {16 vector
> ans;17 vector
temp;18 recurse(root, sum, temp, ans);19 return ans;20 }21 };

 

转载于:https://www.cnblogs.com/linyx/p/3718379.html

你可能感兴趣的文章
云风<<代码大全>>读书笔记
查看>>
JS 类型分类,不同类型值比较的区别
查看>>
iCarousel简介
查看>>
Script:收集Flashback Database Log诊断信息
查看>>
org.apache.tomcat.dbcp.dbcp.SQLNestedException
查看>>
oracle RAC 错误信息 prkh-1010
查看>>
可重复分组报表
查看>>
【Hadoop】- MapReduce 框架详细介绍
查看>>
梦游西藏
查看>>
YUM、RPM 与APT 、DPKG 常用等价命令
查看>>
ubuntu---使用命令行下载文件
查看>>
Servlet Demo
查看>>
Struts2中的<s:action>标签
查看>>
Java中取某一个范围的随机数
查看>>
Linux 进程管理
查看>>
一条复杂SQL实现思路
查看>>
我的友情链接
查看>>
android app 退出时提示确认
查看>>
win10 配置
查看>>
java 编译100个范例
查看>>