leetcode257
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
Stack<TreeNode> S = new Stack<TreeNode>(); List<List<string>> list = new List<List<string>>(); private void postNode(TreeNode root)
{
if (root != null)
{
S.Push(root);
if (root.left != null)
{
postNode(root.left);
}
if (root.right != null)
{
postNode(root.right);
}
if (root.left == null && root.right == null)
{
var l = S.Reverse().ToList();
var n = new List<string>();
foreach (var d in l)
{
n.Add(d.val.ToString());
}
list.Add(n);
}
S.Pop();
}
} public IList<string> BinaryTreePaths(TreeNode root)
{
var result = new List<string>(); postNode(root);
foreach (var d in list)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < d.Count; i++)
{
sb.Append(d[i]);
if (i != d.Count - )
{
sb.Append("->");
}
}
result.Add(sb.ToString());
}
return result;
}
}
https://leetcode.com/problems/binary-tree-paths/#/description
leetcode257的更多相关文章
- [LeetCode257]Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- linux系统时间获取方式
Linux 操作系统计算系统时间:主要函数:time localtime gmtime asctime ctime mktime difftime s ...
- ORACLE导入导出工具的使用
ORACLE导出工具exp的使用: 1.将数据库TEST(远程的数据库必须为连接标志符)完全导出,用户名system,密码manager,导出到D:\daochu.dmp中: exp s ...
- 从 TWAIN 设备中扫描图像
转自(http://yonsm.net/scan-images-from-a-twain-device/) 一.简介 TWAIN 数据源管理程序 (DSM) 工业标准的软件库,用于从静态图像设备提取图 ...
- Java 动态代理与反射机制
java动态代理必须的两个类与两个接口: 首先需要有一个接口(委托者需要实现该接口的方法)示例如下: <pre name="code" class="html&qu ...
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...
- 本地Jmeter脚本部署在Jenkins上 - Windows
一.下载并安装Jenkins(不进行特别的说明) 二.准备好jmeter脚本 三.插件准备:Publish HTML reports 四.开始 1.登录Jenkins后,点击新建任务 2.输入项目名, ...
- js 判断哪个获得焦点
if(document.activeElement.id="txtIdHouse") { } var xx = document.activeElement.id; xx就是现 ...
- 判断设备(PC,安Android,iOS)
//判断是不是PC function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("An ...
- Python流程控制-while循环-for循环
写重复代码 是可耻的行为 -------------- 完美的分割线 -------------- 摘录自:http://www.runoob.com/python/python-loops.htm ...
- Java第二次作业--数组和String类
Deadline: 2017-3-28 23:00 一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握基本数据类型和引用数据类型的区别 理解对象的生成与引用的关系 掌握构造方法的重载 掌握St ...