leetcode144
/**
* 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<int> S = new Stack<int>(); private void preNode(TreeNode node)
{
if (node != null)
{
S.Push(node.val);
if (node.left != null)
{
preNode(node.left);
}
if (node.right != null)
{
preNode(node.right);
}
}
} public IList<int> PreorderTraversal(TreeNode root)
{
preNode(root);
var list = S.Reverse().ToList();
return list;
}
}
https://leetcode.com/problems/binary-tree-preorder-traversal/#/description
leetcode144的更多相关文章
- LeetCode144:Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode144:Binary Tree Preorder Traversal
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...
- Leet-code144. Binary Tree Preorder Traversal
这是一道将二叉树先序遍历,题目不难. 首先采用深搜递归 /** * Definition for a binary tree node. * public class TreeNode { * int ...
- Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class S ...
- leetcode144 longest-palindromic-substring
题目描述 找出给出的字符串S中最长的回文子串.假设S的最大长度为1000,并且只存在唯一解. Given a string S, find the longest palindromic substr ...
- LeetCode144 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? /** * Defin ...
- LeetCode二叉树的前序、中序、后序遍历(递归实现)
本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
随机推荐
- ZOJ 3498 Javabeans(找规律)
Javabeans Time Limit: 2 Seconds Memory Limit: 65536 KB Javabeans are delicious. Javaman likes t ...
- java中常用的帮助类。加快开发速度
数据库帮助类 package com.cwnu.uitl; import java.sql.*; /** * 数据库基础操作实现类 * * @author BlackWinter * * @date ...
- Visual Studio Nuget还原步骤
vs2013是在这里还原: NuGet套件还原步骤(以vs2012为例) 下载别人的范例,出现由于Nuget套件不存在而无法启动时:效果如下图: 步骤如下:1.点击 项目->启用NuGet程序包 ...
- QGrapicsScene类
概述 QgraphicsScene类为管理大量的2D图形item提供了一个管理界面,做为item的容器,它配合使用QgraphicsView使用来观察items,例如线,矩形,文本或者自定义的item ...
- poj3107(dfs,树形dp)
和poj1655的方法完全一样,但是这道题的n的范围大了,用vector存图会TLE(poj没有O2编译优化),所以改用前向星来存图就可以了.. 有关树的重心,看这里:poj1655 这里解释一下前向 ...
- nodejs express project
user root install express npm install express -g install express... npm install express-generator -g ...
- 【如何入门ACM】
第一阶段:先刷水题,水题,就是几乎不牵扯算法.需要自己想方法解决.这样的题,一是锻炼逻辑和思维的严谨,二是锻炼代码能力.一般做到60-200题左右. 第二阶段:渐渐的学一些简单的算法,或者专题训练,或 ...
- HihoCoder 1068 RMQ-ST算法+BIT
以前都是用的BIT或者线段树(前者多一些). 对于ST(Sparse Table),在求倍增or公共祖先(LCA)时见过,说明还有其他用处,所以还是学习一下. 首先是预处理,用动态规划(DP)解决. ...
- Python面对对象相关知识总结
很有一段时间没使用python了,前两天研究微信公众号使用了下python的django服务,感觉好多知识都遗忘了,毕竟之前没有深入的实践,长期不使用就忘得快.本博的主要目的就是对Python中我认为 ...
- elasticsearch 动态模板
在elasticsearch中,如果你有一类相似的数据字段,想要统一设置其映射,就可以用到一项功能:动态模板映射(dynamic_templates). 每个模板都有一个名字用于描述这个模板的用途,一 ...