先序遍历,用递归来做,简单的不能再简单了。代码如下:

(以下仅实现了先序遍历,中序遍历类似,后序遍历和这两个思路不一样,具体详见Binary Tree Postorder Traversal

/**
* 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 {
private:
vector<int> res;
public:
void getOrder(TreeNode* node)
{
if(node==NULL)
return ;
else
{
res.push_back(node->val);
getOrder(node->left);
getOrder(node->right);
}
return ;
}
vector<int> preorderTraversal(TreeNode* root) {
if(root==NULL)
return res;
res.push_back(root->val);
getOrder(root->left);
getOrder(root->right);
return res; }
};

但是题目要求用迭代来做。

迭代的代码如下:

/**
* 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) {
vector<int> res;
stack<TreeNode*> stk;
while(root!=NULL||!stk.empty())
{
if(root!=NULL)
{
res.push_back(root->val);
stk.push(root);
root=root->left;
}
else
{
root=stk.top()->right;
stk.pop();
} }
return res;
}
};

  中序遍历迭代代码如下:

/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk;
while(root!=NULL||!stk.empty())
{
if(root!=NULL)
{
stk.push(root);
root=root->left;
}
else
{
root=stk.top();
res.push_back(root->val);
stk.pop();
root=root->right;
}
}
return res;
}
};

  

Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)的更多相关文章

  1. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  2. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

  3. 【LeetCode】Binary Tree Preorder Traversal

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

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

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

  5. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  6. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  7. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

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

  8. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

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

  9. LeetCode: Binary Tree Preorder Traversal 解题报告

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

随机推荐

  1. POJ 2763 Housewife Wind 纯粹LCA写法(简单无脑)

    Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordina ...

  2. [zz]【整理】Python中Cookie的处理:自动处理Cookie,保存为Cookie文件,从文件载入Cookie

    http://www.crifan.com/python_auto_handle_cookie_and_save_to_from_cookie_file/ #!/usr/bin/python # -* ...

  3. supervisor安装、配置和运行

    supervisor是python写的进程管理工具,supervisor能够批量对进程执行启动,停止,重启等操作,有效提高了运维效率.注意supervisor只能管理前台进程,supervisor会自 ...

  4. 手脱ACProtect V1.4X(有Stolen Code)

    1.载入PEID ACProtect V1.4X -> risco 首先需要说明的是,这个壳被偷取的代码非常多,如果去找的话会比较麻烦,所以我们换一种另类的方法,不脱壳也可以使用资源修改器对程序 ...

  5. svg学习

    百度百科: SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C制定的一种新的二维矢量图形格式,也 ...

  6. 【通用邮件发送】C# QQ 网易邮箱

    using BooksStore.Domain.Models; using System; using System.Collections.Generic; using System.Linq; u ...

  7. Python 模块搜索路径 -- (转)

    最近在看<Python源码剖析>,对Python内部运行机制比以前了解的更深入了,感觉自己有机会也可以做个小型的动态脚本语言了,呵呵,当然是吹牛了.目的当然不是创造一个动态语言,目的只有一 ...

  8. Android中TextView设置字体

    最近项目中出现把字体设置成宋体,微软雅黑,黑体,楷体等的需求; 度娘发现Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace",除此之外还可以使 ...

  9. LCD实验学习笔记(八):中断

    s3c2440有60个中断源(其中15个为子中断源). 31个32位的通用寄存器,6个程序状态寄存器.有6种工作模式(系统/用户模式,快中断模式,管理模式,数据访问中止模式,中断模式,未定指令中止模式 ...

  10. 【转】gif文件格式详解

    1.概述 ~~~~~~~~ GIF(Graphics Interchange Format,图形交换格式)文件是由 CompuServe公司开发的图形文件格式,版权所有,任何商业目的使用均须 Comp ...