LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
1
\
2
/
3
return [1,2,3].
前序遍历二叉树,只不过题目的要求是尽量不要使用递归,当然我还是先用递归写了一个:
/**
* 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<int> preorderTraversal(TreeNode* root) {
ret.clear();
if(root != NULL)
preTrans(root);
return ret;
}
void preTrans(TreeNode * root){
ret.push_back(root->val);
if(root->left != NULL) preTrans(root->left);
if(root->right != NULL) preTrans(root->right);
}
private:
vector<int> ret;
};
当然还有非递归的方法,递归那么当然要使用到stack,其实这种方法写起来有点像是java中的递归的迭代器:
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ret;
if(root == NULL) return ret;
stack<TreeNode*> treeStk;
treeStk.push(root);
TreeNode * tmpNode;
while(!treeStk.empty()){
tmpNode = treeStk.top();
treeStk.pop();
ret.push_back(tmpNode->val);
if(tmpNode->left != NULL) treeStk.push(tmpNode->left);
if(tmpNode->right != NULL) treeStk.push(tmpNode->right);
}
return ret;
}
};
java版本的代码如下所示,首先是递归:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
tranversal(root, ret);
return ret;
}
public void tranversal(TreeNode root, List<Integer> ret){
if(root != null){
ret.add(root.val);
tranversal(root.left, ret);
tranversal(root.right, ret);
}
return;
}
}
然后是非递归的方式:
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> ret = new ArrayList<Integer>();
s.push(root);
while(!s.isEmpty()){
TreeNode t = s.pop();
if(t == null)
continue;
ret.add(t.val);
s.push(t.right); //注意因为使用的是栈,所以应该先push right.
s.push(t.left);
}
return ret;
}
}
LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- [String ] StringBuffer VS StringBuilder
StringBuilder的性能高于StringBuffer,因为StringBuffer是线程安全的. 首先说明一下,一般情况下,字符串相加默认是StringBuilder,但是当数量大于100,或 ...
- MySQL数据库(6)_用户操作与权限管理、视图、存储过程、触发器、基本函数
用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIE ...
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- box-flex兼容写法
box-flex布局在这几年发生了多次变化,可分为2009版.2011版以及2013版, 区分: display:box(inline-box), box-{*}的格式为2009版 display:b ...
- INSPIRED启示录 读书笔记 - 第27章 合理运用瀑布式开发方法
瀑布式开发方法的基本原则 1.采用阶段式开发:软件开发过程被事先分成固定的几个阶段,撰写书面的需求说明文档.设计高层软件架构.设计低层细节.编写代码.测试.部署 2.采用阶段式评审:每个阶段结束后,对 ...
- 中断下半部tasklet【转】
本文转载自:http://edsionte.com/techblog/archives/1547 tasklet的实现 tasklet(小任务)机制是中断处理下半部分最常用的一种方法,其使用也是非常简 ...
- 单文件夹下的C程序如何编写Makefile文件
通过学习已经学会了GCC的一些基础的命令,以及如何将C语言源代码编译成可执行文件. 我们已经知道在linux环境下编译源码时,常会有以下三个步骤: ./configure make make clea ...
- linux环境下的python安装过程(含setuptools)
这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行python的安装. 一.下载python源码包 打开ubuntu下的 ...
- 初识Spring security-无Security的SpringMVC
百度百科定义: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了 ...
- JavaWeb基础
1.Servlet: Servlet是JavaWeb的3大组件之一,是把url请求转为后台处理的具体类,此类必须实现Servlet接口,一把实际使用时无须我们实现,我们直接使用JavaEE的HTTPS ...