博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode path Sum i ,ii递归和迭代解法
阅读量:4187 次
发布时间:2019-05-26

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

/********************************************************************************** * * 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.
  • ************************************************************************/
// Date   : 2016.07.23// Author : yqtao// https://github.com/yqtaowhu/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };class Solution {public:bool hasPathSum1(TreeNode *root, int sum) {        if (root == NULL) return false;        if (root->val == sum && root->left ==  NULL && root->right == NULL) return true;        return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val);    }   bool  hasPathSum2(TreeNode *root, int sum){    if(!n) return false;    stack
st; TreeNode *cur = root, *pre; while(cur || !st.empty()){ while(cur){ st.push(cur); sum -= cur->val; cur = cur->left; } cur = st.top(); if(!cur->left && !cur->right && !sum) return true; if(cur->right && pre != cur->right) cur = cur->right; else{ st.pop(); sum += cur->val; pre = cur; cur = NULL; } } return false;} };
********************************************************************************** * * 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]* ]* *               **********************************************************************************/class Solution {public:    vector
> pathSum(TreeNode* root, int sum) {
vector
> paths; vector
path; findPaths(root, sum, path, paths); return paths; }private: void findPaths(TreeNode* node, int sum, vector
& path, vector
>& paths) { if (!node) return; path.push_back(node -> val); if (!(node -> left) && !(node -> right) && sum == node -> val) paths.push_back(path); findPaths(node -> left, sum - node -> val, path, paths); findPaths(node -> right, sum - node -> val, path, paths); path.pop_back(); }};

转载地址:http://ovdoi.baihongyu.com/

你可能感兴趣的文章
spring bean 的生命周期
查看>>
常用的排序算法
查看>>
浅谈web授权常用策略随机
查看>>
jenkins自动部署随记
查看>>
Shell命令的随记
查看>>
TCP,HTTP,WEBSOCKET随记
查看>>
关于JVM内存区域的组成以及堆内存的回收原理
查看>>
JVM的垃圾回收机制以及回收策略随记
查看>>
生成XML的方法
查看>>
Reactor模式
查看>>
Connector configured to listen on port 8080 failed to start
查看>>
注解的使用
查看>>
【转】inet_addr、inet_aton、inet_pton异同小结
查看>>
linux中绑定80端口失败
查看>>
关于链接失败 对xxxx ‘__gxx_personality_v0’未定义的引用
查看>>
关于char*类型返回值和字符串常量
查看>>
epoll的一些关键点和总结(一)
查看>>
epoll的一些关键点和总结(二)
查看>>
关于epoll边缘触发模式(ET)下的EPOLLOUT触发
查看>>
socket TCP编程中connect的一些坑
查看>>