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,

  1. 5
  2. / \
  3. 4 8
  4. / / \
  5. 11 13 4
  6. / \ / \
  7. 7 2 5 1

return

  1. [
  2. [5,4,11,2],
  3. [5,8,4,5]
  4. ]
  5.  
  6. 题意:给定一数,在树中找出所有路径和等于该数的情况。
    方法一:
    使用vector向量实现stack的功能,以方便输出指定路径。思想和代码和Path sum大致相同。
  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<vector<int> > pathSum(TreeNode *root, int sum)
  13. {
  14. vector<vector<int>> res;
  15. vector<TreeNode *> vec;
  16. TreeNode *pre=NULL;
  17. TreeNode *cur=root;
  18. int temVal=;
  19.  
  20. while(cur|| !vec.empty())
  21. {
  22. while(cur)
  23. {
  24. vec.push_back(cur);
  25. temVal+=cur->val;
  26. cur=cur->left;
  27. }
  28. cur=vec.back();
  29. if(cur->left==NULL&&cur->right==NULL&&temVal==sum)
  30. { //和Path sum最大的区别
  31. vector<int> temp;
  32. for(int i=;i<vec.size();++i)
  33. temp.push_back(vec[i]->val);
  34. res.push_back(temp);
  35. }
  36. if(cur->right&&cur->right !=pre)
  37. cur=cur->right;
  38. else
  39. {
  40. vec.pop_back();
  41. temVal-=cur->val;
  42. pre=cur;
  43. cur=NULL;
  44. }
  45.  
  46. }
  47. return res;
  48. }
  49. };

方法二:递归法

  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<vector<int> > pathSum(TreeNode *root, int sum)
  13. {
  14. vector<vector<int>> res;
  15. vector<int> path;
  16. findPaths(root,sum,res,path);
  17. return res;
  18. }
  19. void findPaths(TreeNode *root,int sum,vector<vector<int>> &res,vector<int> &path)
  20. {
  21. if(root==NULL) return;
  22. path.push_back(root->val);
  23. if(root->left==NULL&&root->right==NULL&&sum==root->val)
  24. res.push_back(path);
  25.  
  26. findPaths(root->left,sum-root->val,res,path);
  27. findPaths(root->right,sum-root->val,res,path);
  28. path.pop_back();
  29. }
  30. };
  1.  

[Leetcode] Path Sum II路径和的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. [LeetCode] 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 ...

  5. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  6. 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 ...

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

  8. [leetcode]Path Sum II @ Python

    原题地址:https://oj.leetcode.com/problems/path-sum-ii/ 题意: Given a binary tree and a sum, find all root- ...

  9. [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 ...

随机推荐

  1. 吐血分享:QQ群霸屏技术(初级篇)

    QQ群,仿似一个冷宫;But,你真摒弃不起. 某人,坐拥2000多个2000人群,月收入10w+,此类人数少,皆因多年的沉淀,以形成完全的壁垒,难以企及的层次. 流量的分散,QQ群相对比较优质的地带, ...

  2. kafka概述

    kafka概述 Apache Kafka是一个开源 消息 系统,由Scala写成.是由Apache软件基金会开发的一个开源消息系统项目. Kafka最初是由LinkedIn开发,并于2011年初开源. ...

  3. eclipse 右键没有Build Path

    如果Project Explorer右键没有build pathWindow ->show view 选择package explorer 参考https://blog.csdn.net/cod ...

  4. keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结

    1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...

  5. 嵌入式Linux系统移植(二)——交叉编译工具集

    常用工具:readelf.size.nm.strip.strings.objdump.objcopy.addr2line readelf:读可执行文件的elf头 ELF Header: Magic: ...

  6. 嵌入式框架Zorb Framework搭建六:定时器的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  7. 关于 spring-aop理解

    对于Aop 一直理解很是不到位  谈谈自己理解! Aop : Aspect: 切面    joinpoint 连接点  pointCut 切点  Advice 增强  targert 目标对象   w ...

  8. 破解PHPStrom 10 and Pycharm

    注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...

  9. linux内存

    在Linux的世界中,从大的方面来讲,有两块内存,一块叫做内存空间,Kernel Space,另一块叫做用户空间,即User Space.它们是相互独立的,Kernel对它们的管理方式也完全不同 驱动 ...

  10. Qt irrlicht(鬼火)3D引擎 摄像机旋转问题

    点击打开链接Irrlicht中的摄像有一个函数 setUpVector() if (m_device != 0 ) { core::vector3df rotation(y,x,0.f); m_cam ...