【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. ...
随机推荐
- Jade之Interpolation
Interpolation jade为不同的需求提供了一些特殊的操作符.详见Github = 将右边的值赋予左边,或者替换为右边变量的值. //- 赋值,js格式即可. - var title = & ...
- document.write()
以前一直以为document.write()就一定会清空文档里面的所有内容,一直没有去尝试,最近才知道原来是要在特定的情况下document.write才会清空文档里面所有内容的,在这里,觉得应该告诉 ...
- Reduce inversion count 求最小逆序数
本问题出自:微软2014实习生及秋令营技术类职位在线测试 (Microsoft Online Test for Core Technical Positions) Description Find a ...
- ORACLE_簽核PROC帶游標
CREATE OR REPLACE PROCEDURE SP_C_TXYYMM(VAPPLY_NO IN VARCHAR2,VUSER_NM IN VARCHAR2,VFAC_NO IN VARCHA ...
- 对angular实现延迟加载template和controller
1.在lib目录中添加 script.js 文件,并在index.html其他<script>之前引用之: <script src="lib/script.js" ...
- javaWeb-mvc之利用c3p0写入数据库出现乱码
在使用c3p0向数据库中写入中文数据时出现乱码,于是我采用了和properties中配置url一样 url=jdbc:mysql://localhost:3306/student?Unicode=tr ...
- 【Win10】UAP/UWP/通用 开发之 SplitView
[Some information relates to pre-released product which may be substantially modified before it's co ...
- Apache Mina(一)
原文链接:http://www.cnblogs.com/xuekyo/archive/2013/03/06/2945826.html Apache Mina是一个能够帮助用户开发高性能和高伸缩性网络应 ...
- 解读SQL Server 2014可更新列存储索引——存储机制
概述 SQL Server 2014被号称是微软数据库的一个革命性版本,其性能的提升的幅度是有史以来之最. 可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性 ...
- JavaScript简洁继承机制实现(不使用prototype和new)
此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不 ...