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. 上传预览 easyui部分控件获取focuse 表单验证

    js: $(document).ready(function () { //$('#creater').combobox({ // url: '/VMS.UI/BindData/ScheamData? ...

  2. WPF dataGrid中的check的改变事件

    关于datagrid信息: <DataGridTemplateColumn Header="备注"> <DataGridTemplateColumn.CellTe ...

  3. return_url和notify_url的区别

    页面跳转同步通知页面特性(return_url特性) (1)   买家在支付成功后会看到一个支付宝提示交易成功的页面,该页面会停留几秒,然后会自动跳转回商户指定的同步通知页面(参数return_url ...

  4. C# WInform 界面左导航菜单

    如图所示: 下载位置: http://pan.baidu.com/s/1c1uRwkw

  5. Java反射机制示例

    链接: http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html package com.stono.reftest; imp ...

  6. eclipse中的Java项目导出成为一个可以直接双击运行的jar文件

    1. 选择要到处JAR文件的工程,右键选择“Export” 2. 选择“Java-->Runnable JAR file”,点击“Next”: 3. 在“Launch configuration ...

  7. 深圳尚学堂:Java中Class对象

    Java中生成Class对象和生成instance都有多种方式.所以只有弄清其中的原理,才可以深入理解.首先要生成Class对象,然后再生成Instance.那Class对象的生成方式有哪些呢,以及其 ...

  8. C# vs TypeScript - 高级类型

    总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...

  9. 蓝桥网试题 java 算法训练 区间k大数查询

    -------------------------------------------------------------------------- 数组也有sort方法 尽量把输入和操作分开写 -- ...

  10. stringBuffer的使用及字符串比较的区别

    /* * 关于equals()和==: 对于String简单来说就是比较两字符串的Unicode序列是否相当,如果相等返回true; * 而==是比较两字符串的地址是否相同,也就是是否是同一个字符串的 ...