题目:

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

Note: Recursive solution is trivial, could you do it iteratively?

说明:

1)递归和非递归实现,其中非递归有两种方法

2)复杂度,时间O(n),空间O(n)

实现:

一、递归

 /**
* Definition for binary tree
* 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) {
vector<int> root_vec;
vector<int> left_vec;
vector<int> right_vec;
if(root==NULL) return root_vec;
root_vec.push_back(root->val);
if(root->left!=NULL) left_vec=preorderTraversal(root->left);
if(root->right!=NULL) right_vec=preorderTraversal(root->right);
root_vec.insert(root_vec.end(),left_vec.begin(),left_vec.end());
root_vec.insert(root_vec.end(),right_vec.begin(),right_vec.end());
return root_vec;
}
};

二、非递归

根据先序遍历的顺序,先访问根节点,再访问左子树,后访问右子树,而对于每个子树来说,又按照同样的访问顺序进行遍历,非递归的实现思路如下:

对于任一节点P,

1)输出节点P,然后将其入栈,再看P的左孩子是否为空;

2)若P的左孩子不为空,则置P的左孩子为当前节点,重复1)的操作;

3)若P的左孩子为空,则将栈顶节点出栈,但不输出,并将出栈节点的右孩子置为当前节点,看其是否为空;

4)若不为空,则循环至1)操作;

5)如果为空,则继续出栈,但不输出,同时将出栈节点的右孩子置为当前节点,看其是否为空,重复4)和5)操作;

6)直到当前节点P为NULL并且栈空,遍历结束。

a、下面代码实现常规,和上面分析过程一致

 /**
* Definition for binary tree
* 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) {
vector<int> preorder_vec;
TreeNode *p=root;//定义用来指向当前访问的节点的指针
if(p==NULL) return preorder_vec;//若为空树,则返回空vector
stack<TreeNode *> treenode_stack;//创建一个空栈
//直到当前节点p为NULL且栈空时,循环结束
while(p||!treenode_stack.empty())
{
//从根节点开始,输出当前节点,并将其入栈,
//同时置其左孩子为当前节点,直至其没有左孩子,及当前节点为NULL
preorder_vec.push_back(p->val);
treenode_stack.push(p);
p=p->left;
//如果当前节点p为NULL且栈不空,则将栈顶节点出栈,
//同时置其右孩子为当前节点,循环判断,直至p不为空
while(!p&&!treenode_stack.empty())
{
p=treenode_stack.top();
treenode_stack.pop();
p=p->right;
}
}
}
};

b、下面代码实现较简洁(个人感觉,不喜勿喷)

 /**
* Definition for binary tree
* 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) {
stack<TreeNode *> preorder_stack;
TreeNode *p=NULL;
vector<int> preorder_vec;
if(root==NULL) return preorder_vec;//若为空树,则返回空vector
preorder_stack.push(root);//当前节点入栈
while(!preorder_stack.empty())
{
p=preorder_stack.top();//栈顶节点出栈、输出
preorder_stack.pop();
preorder_vec.push_back(p->val);
//注意,下面入栈顺序不能错 ,因为先右后左,
//这样出栈时先遍历才是左孩子(左->中->右)
if(p->right) preorder_stack.push(p->right);//若存在右孩子,则入栈
if(p->left) preorder_stack.push(p->left);//若存在左孩子,则入栈
}
return preorder_vec;
}
};

leetcode 题解:Binary Tree Preorder Traversal (二叉树的先序遍历)的更多相关文章

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

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

  2. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  3. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

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

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

  5. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  6. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  7. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

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

  8. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

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

  9. LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

  10. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

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

随机推荐

  1. printf输出字符串的一些格式

    1. 原样输出字符串:    printf("%s", str); 2. 输出指定长度的字符串, 超长时不截断, 不足时右对齐:    printf("%Ns" ...

  2. [转]Android在初始化时弹出popwindow的方法 .

    转自:http://blog.csdn.net/sxsboat/article/details/7340759 留个人备用0.0 Android中在onCreate()时弹出popwindow,很多人 ...

  3. Xcode——创建你自己的Framework

    (注:以下内容是基于Xcode7.2.1操作的,版本不一,可能界面内容不同!) 如果你想将你开发的控件与别人分享,一种方法是直接提供源代码文件.然而,这种方法并不是很优雅.它会暴露所有的实现细节,而这 ...

  4. preventDefault stopPropagation??

    棒棒哒~ event.preventDefault https://developer.mozilla.org/zh-CN/docs/Web/API/Event/preventDefault stop ...

  5. JS阻塞的问题

    常见问题    http://www.zhihu.com/question/23101413   阻塞特性:        JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任 ...

  6. git filename to long问题解决

    在.git/config 下面编辑 [core] longpaths = true

  7. Javascript实现笛卡儿积算法

    在根据商品属性计算SKU时,通常会对商品不同选项的不同属性进行笛卡儿积运算. 这是在NodeJs里的实现版本,目前用在生产环境. function cartesian(elements) { if ( ...

  8. MyEclipse卡死解决

    MyEclipse卡死解决 在用[MyEclipse] 写代码很容易卡死机,尤其是在对JSP文件的<%%>之间写代码的时候,只要一弹出智能提示就立刻卡死,程序失去响应,我以为是MyEcli ...

  9. HITAG 1/2/S

    HITAG S -- 3rd generation HITAG™ family Modulation Read/Write Device to Transponder: 100 % ASK and B ...

  10. Semaphore 和 Mutex

    mutex和semaphore有什么区别呢? mutex是用作互斥的,而semaphore是用作同步的. 也就是说,mutex的初始化一定是为1,而semaphore可以是任意的数, 所以如果使用mu ...