Binary Tree Paths leetcode
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的更多相关文章
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- 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 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- 在ASP.NET MVC3项目中,自定义404错误页面
在Web开发中,用户体验是至关重要的,一个友好的网站自然少不了自定义404错误页面. 让笔者为大家介绍404错误页面在ASP.NET MVC3项目中的配置: 第一步,在项目的Web.config文件中 ...
- JWPlayer使用指南
http://support.jwplayer.com/customer/portal/articles/1499103-quickstart-reference <div id="m ...
- 触摸滑动插件 Swiper
Swiper Swiper 是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端. Swiper中文网里已有详细的使用介绍,我就不多做介绍了. http://www.swiper ...
- ubuntu无法进入桌面的修复
今天的kubuntu更新后停在了启动logo上,无法进入系统界面了. 先在网上找了找,搜到了一个看起来很像的. 1)ubuntu在系统启动logo过后无法进入桌面的处理方法 一般情况下,无法显示桌面, ...
- ios NSString拼接方法总结
NSString* string; // 结果字符串 02 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 03 04 / ...
- WinForm 对话框、流
一.对话框 ColorDialog:颜色选择控件 private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDia ...
- We Chall-Encodings: URL -Writeup
MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...
- asp.net权限认证:HTTP基本认证(http basic)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- std::list 源代码解析
首先声明,下面的讲解都是针对GCC2.9,std::alloc 通过下面的源代码大家可以看到list类的内部成员是一个node,而他的类型是linktype,前面的typedef里面有介绍是一个指针, ...
- Extjs grid 组件
表格面板类Ext.grid.Panel 重要的配置参数 columns : Array 列模式(Ext.grid.column.Columnxtype: gridcolumn) 重要的配置参数 tex ...