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(前序遍历二叉树)的更多相关文章

  1. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  2. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  3. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  4. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  5. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  6. [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历

    关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...

  7. leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

  8. 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 ...

  9. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. 024-Spring Boot 应用的打包和部署

    一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...

  2. 稀疏表示(Sparse Representations)

    1.什么是稀疏表示: 用较少的基本信号的线性组合来表达大部分或者全部的原始信号. 其中,这些基本信号被称作原子,是从过完备字典中选出来的:而过完备字典则是由个数超过信号维数的原子聚集而来的.可见,任一 ...

  3. English Phrases

    @1:Phrases requst sth from/of sb 向某人要求某物 a new lease on life   重获新生.焕发生机 state of the art 最先进的 at th ...

  4. Django设置上传文件夹

    django提供了两种字段类型models.FileField与models.ImageField,用于保存上传文件与图象.这两类字段提供了一个参数'upload_to',用于定义上传文件保存的路径( ...

  5. Centos配置sftp

    sftp配置: ssh -V  使用ssh –V命令来查看openssh的版本,版本必须大于4.8p1,低于这个版本需要升级. 1.添加用户及用户组: groupadd sftp useradd -g ...

  6. 【转】Linux下查看进程打开的文件句柄数

    ---查看系统默认的最大文件句柄数,系统默认是1024 # ulimit -n 1024 ----查看当前进程打开了多少句柄数 # lsof -n|awk '{print $2}'|sort|uniq ...

  7. css小知识---input输入块

    对于如下的界面,介绍一种实现方式. 可以将整个界面分为三块,左中右.通过display: inline-block;和float: right;左右浮动效果实现. 代码如下: <!DOCTYPE ...

  8. Oracle索引(1)概述与创建索引

    索引是为了提高数据检索效率而创建的一种独立于表的存储结构,由Oracle系统自动进行维护. 索引的概述        索引是一种可选的与表或簇相关的数据库对象,能够为数据的查询提供快捷的存储路径,减少 ...

  9. 【HackerRank】Ice Cream Parlor

    Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N ...

  10. gstreamer——文档/资源/使用

    http://gstreamer.freedesktop.org/src/ http://gstreamer.freedesktop.org/data/doc/gstreamer/head/qt-gs ...