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. Windows系统Python 安装第三方模块时,提示pip版本有问题

    如果按照提示输入python -m pip install --upgrade pip 还不行, 那么执行easy_install --upgrade pip 即可 参考:https://stacko ...

  2. AWK Demo

    AWK is so poweful. AWK功能太强大了: @1:处理格式化文本(感觉主要还是以这个为主) @2:对文本逐行进行处理(过滤某些行,保留符合条件的) @3:将文件根据不同的条件分成多个文 ...

  3. MVC4 中使用 Area 和 注意的地方

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下:  项目 –> 添加 -> 区域 (Area)  输入 Admin 添加成功后 Area包含:创建一个空 ...

  4. 使用idea2016导出web项目war包

    第一步配置Web Application:Exploded(已经配置的可以跳到第二步): 打开project structure(默认的快捷键是Ctrl+Alt+Shift+S),依次选择Artifa ...

  5. JDK1.8(JRE)和eclipse-jee不匹配解决放

    想要用eclipse-jee的话,需要jdk1.8一下版本才能用. 1.需要下载jdk1.7 2.把jdk1.7安装(不需要设置环境变量). 3.在项目上右击选择properties 4.选择Java ...

  6. 模块调用,datetime,time,logging,递归,双层装饰器, json,pickle迭代器和生成器

    一.python模块(导入,内置,自定义,开源) 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python ...

  7. Loadrunder之脚本篇——关联函数对话框详解

    Insert->New Step,打开Add Step对话框 选择函数web_reg_save_param,点击OK,打开关联函数设置窗口 说明: Parameter Name 此处设置存放参数 ...

  8. 深入浅出Node.js(上)

    (一):什么是Node.js Node.js从2009年诞生至今,已经发展了两年有余,其成长的速度有目共睹.从在github的访问量超过Rails,到去年底Node.jsS创始人Ryan Dalh加盟 ...

  9. 使用awk来提取内容

    1.提取gff文件中的HLA基因的相关bed文件. gff的格式: zcat *gz|gawk 'BGIN{FS="\t";OFS="\t"}$3==" ...

  10. 什么是make config,make menuconfig,make oldconfig,make xconfig,make defconfig,make gconfig?【转】

    本文转载自;https://blog.csdn.net/baweiyaoji/article/details/52876701 在进行内核配置,或者是对一些软件的配置和编译中,常常会遇到: make ...