【LeetCode】257. Binary Tree Paths
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的更多相关文章
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
随机推荐
- Oracle中改变表的Owner和tablespace
初用Oracle,很多的不熟悉,建完库,没有建用户,也没创建表空间,就直接system用户建表添加数据,几个月过去,表建了近百个,数据添加了几万条,才越来越觉得这种方式缺点太多: 在PL/SQL中系统 ...
- 如何获取eID——公安部发行的网络实名认证方式
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- DM9000网卡驱动接受数据从中断方式改成NAPI方式小记
平台是最最经典的s3c2440了,说了要做这件事情很久了,就是改几行代码,一直没有做.前几天逼了自己一下,终于给做了,拖延症患者伤不起. 以下是需要读者对napi机制有所熟悉: step1:在boar ...
- Java中获取路径的各种方法
1. java文件中获得路径 Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.cl ...
- PHPCMS联动菜单的调用函数get_linkage方法详解
v9联动菜单调用方法[注意此为内容页调用方法 {get_linkage($areaid,1,' >> ',1)} 显示效果: 湖北省 >> 武汉市 >> 汉阳区 [ ...
- Fire uva 11624
题目连接:http://acm.hust.edu.cn/vjudge/problem/28833 /* 首先对整个图bfs一次得到火焰燃烧的时刻表 之后在bfs搜路径时加一个火烧表的判断 坑点在于:如 ...
- 打包解决方案后,安装时提示只能在IIS5.1以上运行解决方法
环境:vs2010 sp1,mvc4,WIN10 生成安装项目后进行安装提示: 此安装程序需要Internet Information Server 5.1或更高版本和Windows XP和更高的安装 ...
- [游戏模版14] Win32 键盘控制
>_<:compared with the previous article,this one only adds key-message listener. >_<:up d ...
- ueditor编辑器和at.js集成
源码: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&qu ...
- 简单理解ECMAScript2015中的Promise
ECMAScript6中新增了Promise对象, 所谓Promise对象,即代表着一个还未完成,但将来某时会完成的操作(通常是异步操作).使用Promise对象,我们就可以避免陷入函数层层嵌套的‘回 ...