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. Java线程:什么是线程

    一 基本概念 多任务:同一时刻运行多个程序的能力.每一个任务称为一个线程.可以同时运行一个以上线程的程序称为多线程程序. Java编写程序都运行在在Java虚拟机(JVM)中,在JVM的内部,程序的多 ...

  2. 在ASP.NET MVC中使用JQ插件datatable

    1. Models public class Citys { public int Id { get; set; } public string CityName { get; set; } publ ...

  3. 关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理

    我们在利用SqlBlukcopy技术倒2010 或者2007格式的文件到SqlServer 数据库的时候,会发生如下错误: 原因如下: 1.在用SQL SERVER 2005访问.xlsx文件(off ...

  4. 搭建Node.js开发IDE环境WebStrom5 多图

    1.下载前期准备 node.js下载地址 http://nodejs.org/ WebStrom5下载地址: http://download-ln.jetbrains.com/webide/WebSt ...

  5. 小谈JavaScript中this的用法

    "this"关键字是JavaScript中广泛应用的一种特性,但它经常也是这门语言中最容易混淆和误解的特性.那么"this"的实际意义是什么?它是如何求值的? ...

  6. [nodejs] day1-创建服务器

    一.使用匿名函数(新建文件service.js)创建一个服务器: var http = require("http"); //Node.js自带的 http 模块,并且把它赋值给 ...

  7. C++编程练习(11)----“图的最短路径问题“(Dijkstra算法、Floyd算法)

    1.Dijkstra算法 求一个顶点到其它所有顶点的最短路径,是一种按路径长度递增的次序产生最短路径的算法. 算法思想: 按路径长度递增次序产生算法: 把顶点集合V分成两组: (1)S:已求出的顶点的 ...

  8. Jenkins的新建job和配置job

    这里,我们说一下如何新建并且配置一个job,Jenkins的工作其实有很多都是靠job来完成的,job有很多的功能,这里我们只介绍如何新建和配置一个建构项目的job. 新建job          新 ...

  9. PickerController 添加照片---iOS

    前言 添加照片我们常用的地方有,更换头像,发布状态,朋友圈的时候等等,那我们接下来看看怎么添加上照片吧~ github: 效果图: 正文 1.你可以直接写,也可以声明一个属性.我习惯声明一个属性. @ ...

  10. matlab 子函数的使用

    本文参考了该篇博客:http://www.cnblogs.com/MarshallL/p/4048846.html 对其进行学习,为我所用吧. 一. 在matlab的函数定义中,如果函数如果函数较长或 ...