/**
* 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的更多相关文章

  1. [LeetCode257]Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  2. [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 ...

  3. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  4. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. UVA-11280 Flying to Fredericton (dijkstra)

    题目大意:一张有向图,n个节点,m条边,有边权.求从起点到终点在最多经过s个中间节点(不包括始末点)时的最小权和. 题目分析:因为起点和终点是固定的,只需一次dijkstra打出表dis[u][k], ...

  2. xssProject在java web项目中应用

    注:转载http://337027773.blog.163.com/blog/static/54376980201451133534157/ 1.项目引入xssProtect-0.1.jar.antl ...

  3. this 知多少

    js 中的this 到底指向哪里涅,学习js的过程中,肯定有不少小伙伴们调入this的大坑中,究其原因,this的指向在函数创建的时候是决定不了的,在调用的时候才能决定,谁调用的就指向谁...曾经,我 ...

  4. Java LRU的实现

    最近在leetcode上做题的时,看到了一道有关LRU Cache的题目,正好我当初面试阿里巴巴的时候问到的.主要采用linkedHashMap来实现. package edu.test.algori ...

  5. iOS-----简易地CocoaAsyncSocket使用

    CocoaAsyncSocket使用 代理的.h文件 //GCDAsyncSocketDelegate执行代理对象 #import <Foundation/Foundation.h> #i ...

  6. PyalgoTrade 优化(六)

    满足优化器组件.这个想法很简单: 有一个服务器负责: 提供数据来运行策略. 提供运行策略的参数. 记录每个工作线程的策略结果. 有多名工作人员负责: 使用服务器提供的数据和参数运行策略. 为了说明这一 ...

  7. Yii隐藏单入口

    Yii进入项目首页时默认是index.php文件路径,如何把index.php去掉,方法如下: 打开apache配置文件http.conf,找到如下的代码: #LoadModule rewrite_m ...

  8. C#修改注册表

    某次需要使用C#对注册表进行操作,不过却发现没有权限,研究了以下发现是当前系统用户的问题.除非当前系统用户是Administrator,否则就会给你抛出一个异常.后来在网上发现了一个方法,原来C#也可 ...

  9. Anatoly and Cockroaches

    Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...

  10. 堆排序(C语言实现)

    一.堆的概念 所谓堆,它是一个数组,也能够被看成一个近似的全然二叉树.树上每一个结点相应数组的一个元素.二叉堆分为二种:最大堆和最小堆.本文主要介绍最大堆,最小堆类似.最大堆的特点:对于随意某个结点, ...