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 ... 
随机推荐
- .NET DLL 保护措施详解(非混淆加密加壳)核心思路的实现
			最近有很多朋友通过BLOG找到我询问我的相关细节,其实相关的实现细节我早已把源码上传到51aspx上面了,地址是http://www.51aspx.com/code/codename/56949 也有 ... 
- 我用Cocos2d-x模拟《Love Live!学院偶像祭》的Live场景(二)
			上一章分析了Live场景中各个元素的作用,这一章开始来分析最关键的部分——打击物件的实现. 上一章放出的视频很模糊,先来看一个清晰版的复习一下:http://www.bilibili.com/vide ... 
- Spring3.2AOP实现需要添加的三个包
			Spring3.2AOP实现需要添加的三个包 http://down.51cto.com/data/1001395 http://down.51cto.com/data/519542 
- 告别被拒,如何提升iOS审核通过率(下篇)——应用内容检查大法与提审资源检查大法
			WeTest 导读 之前的<告别被拒,如何提升iOS审核通过率(上篇)>分享了客户端检查的相关要点,本篇会给大家介绍有关应用内容的检查项和提审资源相关检查项要点. 应用内容检查大法 苹果对 ... 
- 在VMWare虚拟机中安装Ubuntu 16.04.1 LTS
			一.需要的准备 安装好VMWare虚拟机(傻瓜式安装,一直next就可以,请支持正版),将Ubuntu的系统镜像下载好,目前最新的LTS版本为16.04.1. 我把虚拟机和Ubuntu镜像传到了百度云 ... 
- .net 开发经理的月薪
			因为各人的成长不一样,有人工作了5年,技术也只能当个高级程序员,有人工作了两年,就能带领一个团队,有人在初中时期就写了一个很牛X 的框架,而我工作也快5年,也努力奋斗了5年,我觉得自己有能力做开发经理 ... 
- 使用SpringMvc调用POI jar导出excel的源码
			@RequestMapping(value = "/result/export") public String export(ResultIn in,HttpServletRequ ... 
- js详解之作用域-实例
			函数如下大家可以做做看 function aa(a,b,c){ function a(){} console.log(a); console.log(aa); console.log(argument ... 
- java_JDBC(3)
			Batch和Fetch两个特性非常重要.Batch相当于JDBC的写缓冲,Fetch相当于读缓冲 如果把JDBC类比为JAVA IO的话,不使用Fetch和Batch相当于直接使用FileInputS ... 
- Picasso 修改缓存路径
			Picasso 是 Square 公司开源的一个非常友好的图片加载框架,使用范围也比较广泛.具体的使用这里就不做介绍了,文章主要讲讲如何修改图片的缓存路径.Picasso默认的缓存路径位于data/d ... 
