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

For example, given the following binary tree:

   1
/ \
2 3
\
5

All root-to-leaf paths are:

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

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

很简单的题目,二叉树的遍历,为了追求挑战性,我选择非递归实现

vector<string> binaryTreePaths(TreeNode* root) {
vector<string> out;
if (root == nullptr)
return out;
vector<TreeNode*> sta;
sta.push_back(root);
TreeNode* lastRoot = root;
while (!sta.empty())
{
root = sta.back();
if (lastRoot != root->right)
{
if (lastRoot != root->left) {
if (root->left != nullptr) {
sta.push_back(root->left);
continue;
}
}
if (root->right != nullptr) {
sta.push_back(root->right);
continue;
}
else if (root->left == nullptr)
{
string str(to_string(sta[]->val));
for (int i = ; i < sta.size(); ++i)
str.append("->" + to_string(sta[i]->val));
out.push_back(str);
}
}
lastRoot = root;
sta.pop_back();
}
return out;
}

至于DFS深度遍历,还是老老实实的使用visited标记记录每个节点吧。上面的方法只适用于二叉树

 

Binary Tree Paths leetcode的更多相关文章

  1. 【LeetCode】257. Binary Tree Paths

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

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

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

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

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

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

  5. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

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

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

  7. LeetCode 257. Binary Tree Paths (二叉树路径)

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

  8. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

随机推荐

  1. Oracle 11gR2 RAC ohasd failed to start 解决方法

    rcrCRS-4124: Oracle High Availability Services startup failed. CRS-4000: Command Start failed, or co ...

  2. HTML5 Canvas、内联 SVG、Canvas vs. SVG

    canvas 元素用于在网页上绘制图形. 什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canv ...

  3. cgLib生成动态代理

    package com.stono.cglib; import java.lang.reflect.Method; import net.sf.cglib.proxy.Enhancer; import ...

  4. Flex移动应用程序开发的技巧和窍门(二)

    范例文件 flex-mobile-dev-tips-tricks-pt2.zip 这是关于Flex移动应用程序开发的技巧和窍门的一系列文章中的第二部分.第一部分 内容主要集中讨论了视图之间以及应用程序 ...

  5. spring mvc 与 jasper Report集成

    http://blog.csdn.net/jia20003/article/details/8471169 注意其中的图片地址说明: 如果有子报表,也会到class文件夹中去寻找: 如果子报表有路径的 ...

  6. Swiper.js使用遇到的问题总结onSlideChangeEnd回调偶尔触发,偶尔不触发等;

    Swiper 是一个开源免费的移动触摸插件. 在使用中遇到这样一个问题,记录一下. page 间切换效果 使用 fade 的时候,如果每个页面的大小不一样, 比如第一个页面全屏, 第二个页面比第一个小 ...

  7. JDK源码分析-Integer

    Integer是平时开发中最常用的类之一,但是如果没有研究过源码很多特性和坑可能就不知道,下面深入源码来分析一下Integer的设计和实现. Integer: 继承结构: -java.lang.Obj ...

  8. 走进React

    走进React React是一个构建用户界面的JavaScript库,是Facebook公司在2013年5月在github上开源的.其特点如下: 高效--React通过对DOM的模拟,最大程度地减少和 ...

  9. iOS 多线程NSThread理解与场景示例

    NSThread是相对GCD和NSOperationQuene而言,比较轻量级的一种多线程处理方式. 但同时,它的弊端就是需要自己管理线程的生命周期,以及线程同步:而另外两种不需要自己管理. 常见方法 ...

  10. Graphql入门

    Graphql入门 GraphQL是一个查询语言,由Facebook开发,用于替换RESTful API.服务端可以用任何的语言实现.具体的你可以查看Facebook关于GraphQL的文档和各种语言 ...