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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 最短路径求解(Dijkstra)
Dijkstra算法分析 题目分析参照<数据结构>(严蔚敏)7-6节 最短路径问题描述 参照日常生活中的公交查询系统.我们有选项: 少换乘/最少站数 价格最少/时间最短.... (ps:下 ...
- VAE--就是AutoEncoder的编码输出服从正态分布
花式解释AutoEncoder与VAE 什么是自动编码器 自动编码器(AutoEncoder)最开始作为一种数据的压缩方法,其特点有: 1)跟数据相关程度很高,这意味着自动编码器只能压缩与训练数据相似 ...
- 多个数值转QString
int, float, double等数值类型转换为QString的方法 1. 用QTextStream QTextStream类可以用数据流的方式直接将任意多个数值.字符.字符串等传入QString ...
- bzoj2163
题解: 拆点网络流 然后用总和-最大流 代码: #include<iostream> #include<cstring> #include<cstdio> #inc ...
- python3 中文乱码,UnicodeEncodeError: 'latin-1' codec can't encode characters in position 10-13: ordinal not in range(256)
将其源代码复制下来运行之后,报了下面这个错误: UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-13 ...
- timer Compliant Controller project (2)--Project Demonstration
1software flow diagram As we know, Embedded design is the core of Electronic Product Design. Di ...
- JS数组中级+高级技巧
本文介绍JS数组一些比较进阶的方法: reverse:数组反转: join:(参数)以参数为连接符将数组拼接为字符串: 实例: var arr=[]; arr[3]="haha"; ...
- Oracle:"ORA-00942: 表或视图不存在"
情景 项目中使用Powerdesigner设计数据结构,在Powerdesigner中数据表和字段都区分了大小写,并生成了Oracle表,在执行Sql脚本时遇到以下问题:“ORA-00942: 表或视 ...
- 关于location.href赋值的php用法
<?php echo $_GET['action']; ?> <!doctype html> <html lang="zh"> <head ...
- 使用位图文本工具BMFont从图片生成自定义字体
bmfont工具如何使用 http://www.360doc.com/content/13/1206/12/14253074_334930801.shtml fnt各属性含义 http://www.2 ...