Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

  1. 1
  2. / \
  3. 2 3
  4. \
  5. 5

All root-to-leaf paths are:

  1. ["1->2->5", "1->3"]

思路:用两个stack<TreeNode*> in , s;

in : 记录当前的路径 p  , 和vector<int>path 相同,只不过一个记录的是 val ,一个记录点的指针。

s  : 记录每个节点的  p->right

v2s : 将path转换称符合要求的string

  1. /**
  2. * Definition for a binary tree node.
  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:string v2s(vector<int>a){
  12. const int len = a.size();
  13. string re = "";
  14. char *p = new char[];
  15. for (int i = ; i<len; i++){
  16. stringstream ss; // 需要包含头文件 #include<sstream>
  17. string temp;
  18. ss << a[i];
  19. ss >> temp;
  20. re = re + temp;
  21. if (i != len - )
  22. re = re + "->";
  23. }
  24. return re;
  25. }
  26. vector<string> binaryTreePaths(TreeNode* root) {
  27. vector<int> path;
  28. vector<string> re;
  29. if (root == NULL) return re;
  30. TreeNode * p = root;
  31. stack<TreeNode*> in, s;
  32. while (p != NULL){
  33. if (p->left == NULL&&p->right == NULL){
  34. in.push(p);
  35. path.push_back(p->val);
  36. re.push_back(v2s(path));
  37. if (s.empty())
  38. return re;
  39. else {
              // 通过while循环,查找下一个需要遍历的点
  40. while (!in.empty() && !s.empty() && (in.top())->right != s.top()){
  41. in.pop();
  42. path.erase(path.end()-);
  43. }
  44. p = s.top();
  45. s.pop();
  46. }
  47. }
  48. else if (p->left == NULL&&p->right != NULL){
  49. path.push_back(p->val);
  50. in.push(p);
  51. p = p->right;
  52. }
  53. else if (p->left != NULL&&p->right == NULL){
  54. path.push_back(p->val);
  55. in.push(p);
  56. p = p->left;
  57. }
  58. else{
  59. path.push_back(p->val);
  60. in.push(p);
  61. s.push(p->right);
  62. p = p->left;
  63. }
  64. }
  65. return re;
  66. }
  67. };

另一道相似题目

有一棵二叉树,树上每个点标有权值,权值各不相同,请设计一个算法算出权值最大的叶节点到权值最小的叶节点的距离。二叉树每条边的距离为1,一个节点经过多少条边到达另一个节点为这两个节点之间的距离。

给定二叉树的根节点root,请返回所求距离。

思路:只需要将保存的路径进行比较,相同的去掉然后相加,就能算出路径长度。

  1. /*
  2. struct TreeNode {
  3. int val;
  4. struct TreeNode *left;
  5. struct TreeNode *right;
  6. TreeNode(int x) :
  7. val(x), left(NULL), right(NULL) {
  8. }
  9. };*/
  10. class Tree {
  11. public:
  12. int getDis(TreeNode* root) {
  13. // write code here
  14. stack<TreeNode*> max, min;
  15. int m = INT_MIN, n = INT_MAX, re = 0;
  16. stack<TreeNode*> in, s;
  17. TreeNode *p = root;
  18. while (p != NULL){
  19. if (p->left == NULL&&p->right == NULL){
  20. in.push(p);
  21. if (p->val > m){
  22. max = in;
  23. m = p->val;
  24. }
  25. if (p->val < n){
  26. min = in;
  27. n = p->val;
  28. }
  29. while (!in.empty() && !s.empty() && (in.top())->right != s.top()){
  30. in.pop();
  31. }
  32. if (s.empty())
  33. break;
  34. else {
  35. p = s.top();
  36. s.pop();
  37. }
  38. }
  39. else if (p->left == NULL&&p->right != NULL){
  40. in.push(p);
  41. p = p->right;
  42. }
  43. else if (p->left != NULL&&p->right == NULL){
  44. in.push(p);
  45. p = p->left;
  46. }
  47. else {
  48. in.push(p);
  49. s.push(p->right);
  50. p = p->left;
  51. }
  52. }
  53. stack<TreeNode*> t1, t2;
  54. while (!max.empty()){
  55. t1.push(max.top());
  56. max.pop();
  57. }
  58. while (!min.empty()){
  59. t2.push(min.top());
  60. min.pop();
  61. }
  62. while (!t1.empty() && !t2.empty()){
  63. if (t1.top() != t2.top())
  64. break;
  65. else
  66. t1.pop(); t2.pop();
  67. }
  68. re = re + t1.size() + t2.size();
  69. return re;
  70. }
  71. };

  

leetcode : Binary Tree Paths的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. LeetCode——Binary Tree Paths

    Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...

  3. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  4. LeetCode Binary Tree Paths(简单题)

    题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...

  5. leetcode Binary Tree Paths python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  6. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  7. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  8. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  9. LeetCode_257. Binary Tree Paths

    257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...

随机推荐

  1. DataSet转化为实体集合类

    /// <summary> /// DataSet转换为实体类 /// </summary> /// <typeparam name="T">实 ...

  2. 解决EP拒绝访问注册表Global键的的问题

    问题描述   打开EP站点时出现如下Error: Message: An unhandled error has occurred. To view details about this error, ...

  3. Newtonsoft.Json异常处理

    var resultStr="{\"Success\":false,\"Data\":1}"; GetRequestPriceRendaIn ...

  4. android studio 1.0 开发 ndk 调用 c++ so库

    一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟 ...

  5. Linux查看CPU和内存使用情况(转)

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态会 ...

  6. UDS(ISO14229-2006) 汉译(No.2参考标准)

    下列参考文件对本文件的系统是不可或缺的.注明日期的参考,仅关于对其引用的版本适用.未注明日期的,仅最新引用的文档(包括任何修改)适用. ISO 7498-1,信息技术——开放系统互联(OSI)——基本 ...

  7. JVM垃圾回收(GC)原理

    一.基本垃圾回收算法 1.引用计数(Reference Counting) 比较古老的回收算法.原理是此对象有一个引用则增加一个引用计数,删除一个引用则较少一个引用计数.垃圾回收时,只回收引用计数为0 ...

  8. Html之 IFrame使用,注意几点

    0x01 iframe的跳出框架 0x02 iframe样式设置 0x03 iframe重置高度 1.首先来一个,跳出iframe的好方法,直接可以在Login.aspx页面使用. if (windo ...

  9. css实现图片切换

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...

  10. SharePoint 2013 User Profile Services之跨场设置

    这段时间有个客户需要在不同SharePoint场中使用网站.文档和用户关注功能.但实际使用中发现默认的关注功能不能跨场使用,这也引出了我接下来的博客,我将在博客中详细描述整个过程. 因为“关注”功能是 ...