Binary Tree Paths

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.

深度优先遍历,每遇到叶节点,将栈中路径记录下来,最后将所有路径转成所需格式

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> path;
if(root == NULL)
return path;
vector<vector<int> > pathv;
unordered_map<TreeNode*, bool> visited;
stack<TreeNode*> stk;
stk.push(root);
visited[root] = true;
if(root->left == NULL && root->right == NULL)
save(pathv, stk);
while(!stk.empty())
{
TreeNode* top = stk.top();
if(top->left && visited[top->left] == false)
{
stk.push(top->left);
visited[top->left] = true;
if(top->left->left == NULL && top->left->right == NULL)
save(pathv, stk);
continue;
}
if(top->right && visited[top->right] == false)
{
stk.push(top->right);
visited[top->right] = true;
if(top->right->left == NULL && top->right->right == NULL)
save(pathv, stk);
continue;
}
stk.pop();
}
return convert(pathv);
}
void save(vector<vector<int> >& pathv, stack<TreeNode*> stk)
{
vector<int> cur;
while(!stk.empty())
{
TreeNode* top = stk.top();
cur.push_back(top->val);
stk.pop();
}
reverse(cur.begin(), cur.end());
pathv.push_back(cur);
}
vector<string> convert(vector<vector<int> >& pathv)
{
vector<string> path;
for(int i = ; i < pathv.size(); i ++)
{
string cur;
cur += to_string(pathv[i][]);
for(int j = ; j < pathv[i].size(); j ++)
{
cur += "->";
cur += to_string(pathv[i][j]);
}
path.push_back(cur);
}
return path;
}
};

【LeetCode】257. Binary Tree Paths的更多相关文章

  1. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

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

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

  3. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  6. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  7. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  8. LeetCode OJ 257. Binary Tree Paths

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

  9. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

随机推荐

  1. python基础语法(4)

    十.Python标准库 Python标准库是随Pthon附带安装的,包含了大量极其有用的模块. 1. sys模块 sys模块包含系统对应的功能 sys.argv ---包含命令行参数,第一个参数是py ...

  2. React Native填坑之旅--HTTP请求篇

    如果不能从头到尾的建立一个RN应用,那么RN将失色不少.本以为HTTP请求部分需要使用Native的实现,Android和iOS各回各家,各调各库了.Google了一下之后居然RN可以使用fetch库 ...

  3. 学习angular 指令构造器时遇到的小问题

    在学习angular时,使用模块来为应用添加自己的指令时,遇到了一个问题,演示的代码如下: <!DOCTYPE html> <html> <head> <me ...

  4. CentOS6.5 解压安装 二进制分发版 mysql-5.5.49-linux2.6-x86_64.tar.gz

    环境:CentOS 6.5 64位 1.下载安装包 http://dev.mysql.com/downloads/mysql/5.5.html#downloads http://dev.mysql.c ...

  5. [ASE][Daily Scrum]11.25

    View Shilin Liu 修复现有的bug,子弹无法破坏射程内视野外的墙 Yiming Liao 无法看见从视野外到视野内的子弹     Server Songtao He 服务器处理子弹命中t ...

  6. Linux Kernel 3.11 正式版发布

    Linus 发布 了 3.11 版本的 Linux 内核.该版本值得关注的新特性有: Lustre 分布式文件系统.透明的 ARM 架构的大数据页支持:ARM64 上的 Xen 和 KVM 虚拟化:O ...

  7. ASP.NET MVC请求处理管道生命周期的19个关键环节(13-19)

    在上一篇"ASP.NET MVC请求处理管道生命周期的19个关键环节(7-12) ",体验了7-12关键环节,本篇继续. ⒀当请求到达UrlRoutingModule的时候,Url ...

  8. 初学者利用git 上传代码到Coding的简单操作步骤

    1.首先登陆coding网站注册账号https://coding.net/ (注册完后需登陆邮箱激活邮件) 2.登陆刚注册的coding账号 ,添加项目 添加项目—〉输入项目名称—〉输入对项目的简单描 ...

  9. 分享我用Qt开发的应用程序【二】在Qt应用程序中使用字体图标fontawesome

    为了使用简单,需要先写一个单件类,头文件的代码如下: 其中静态方法Instance保证IconHelper的实例全局唯一 (注意构造函数已经私有化了) #ifndefICONHELPER_H #def ...

  10. 理解assign,copy,retain变strong

    举个例子: NSString *houseOfMM = [[NSString alloc] initWithString:'装梵几的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一 ...