#include <vector>
#include <stack>
#include <queue>
using namespace std; struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x):val(x),left(nullptr),right(nullptr){};
}; void preOrder(TreeNode* root,vector<int>& res){
if(root == nullptr) return;
res.push_back(root->val);
preOrder(root->left,res);
preOrder(root->right,res);
} void inOrder(TreeNode* root,vector<int>& res){
if(root == nullptr) return;
inOrder(root->left,res);
res.push_back(root->val);
inOrder(root->right,res);
} void postOrder(TreeNode* root,vector<int>& res){
if(root == nullptr) return;
postOrder(root->left,res);
postOrder(root->right,res);
res.push_back(root->val);
} vector<int> preOrder(TreeNode* root){
vector<int> res;
if(root == nullptr) return res; stack<TreeNode*> st;
TreeNode* cur = root;
while(cur || !st.empty()){
while(cur){
res.push_back(cur->val);
st.push(cur);
cur = cur->left;
}
if(!st.empty()){
cur = st.top();
st.pop();
cur = cur->right;
}
}
return res;
} vector<int> inOrder(TreeNode* root){
vector<int> res;
if(root==nullptr) return res; stack<TreeNode*> st;
TreeNode* cur = root;
while(cur || !st.empty()){
while(cur){
st.push(cur);
cur = cur->left;
}
if(!st.empty()){
cur = st.top();
st.pop();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
} vector<int> postOrder(TreeNode* root){
vector<int> res;
if(root == nullptr) return res; stack<TreeNode*> st;
TreeNode* cur = root;
while(cur){
st.push(cur);
cur = cur->left;
} TreeNode* lastVisited = nullptr;
while(!st.empty()){
cur = st.top();
st.pop();
if(cur->right == nullptr || cur->right == lastVisited){
res.push_back(cur->val);
lastVisited = cur;
}else{
st.push(cur);
cur = cur->right;
while(cur){
st.push(cur);
cur = cur->left;
}
}
}
return res;
} vector<int> levelOrder(TreeNode* root){
vector<int> res;
if(root == nullptr) return res; queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
size_t n = q.size();
for(size_t i=;i<n;i++){
TreeNode* cur = q.back();
q.pop();
res.push_back(cur->val);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
}
return res;
}

leetcode(144,94,145,102)中迭代版的二叉树的前、中、后、层级遍历的更多相关文章

  1. leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树

    题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...

  2. java实现二叉树的前中后遍历(递归和非递归)

    这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...

  3. Qt实现 动态化遍历二叉树(前中后层次遍历)

    binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...

  4. Java 非递归实现 二叉树的前中后遍历以及层级遍历

    class MyBinaryTree<T> { BinaryNode<T> root; public MyBinaryTree() { root = new BinaryNod ...

  5. Binary Tree Traversal 二叉树的前中后序遍历

    [抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...

  6. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  7. 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树

    出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...

  8. LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

    题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...

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

    题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...

随机推荐

  1. 1. Swift基本变量|运算符|控制流

    Swift基于cocoa Touch框架,苹果官方为了保证Swift的可靠性,结合多种语言的特性,同时独立了一套属于自己的单独语言,结合了C,C++,OC,Java等语言. 基本变量: 1 . swi ...

  2. Android:Butter Knife 8.0.1配置

    github地址:https://github.com/GarsonZhang/butterknife Butter Knife Field and method binding for Androi ...

  3. Ubuntu快捷键

    https://linux.cn/article-3025-1.html 超级键操作 1.超级键(Win键)–打开dash. 2.长按超级键– 启动Launcher.并快捷键列表. 3.按住超级键,再 ...

  4. 基于gulp 的前端自动化构建方案总结

    一,基础篇 先安装nodejs 使用淘宝镜像安装tnpm 安装 cnpm 插件:npm install -g cnpm --registry=https://registry.npm.taobao.o ...

  5. Monkey测试的策略和分析

    Monkey测试针对不同的对象和不同的目的采用不同的测试方案,首先测试的对象.目的及类型如下: 测试的类型分为:应用程序的稳定性测试和压力测试 测试对象分为:单一apk和apk集合 测试的目的分为:解 ...

  6. 常用JavaBean:JdbcBean codes:Java通过JDBC 连接 Mysql 数据库

    package bean;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class JdbcBean { publi ...

  7. AS3全局与局部坐标转换

    在大部分需要用户点击的游戏中,坐标的转换是一种必须熟练掌握的方法. 首先在一个700x700的舞台中创建2个方块,红色的大方块A是600x600,位于(50,50),绿色的小方块B是300x300.A ...

  8. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. AngularJS: 'Template for directive must have exactly one root element' when using 'th' tag in directive template

    .controller('HomeController', function($scope,$location) { $scope.userName='天下大势,为我所控!'; $scope.clkU ...

  10. docker初学笔记

    什么是docker 不准确的说,docker是一种轻量级的虚拟机,它把可执行文件和运行环境打包成一个image,放在容器里运行,但是启动速度比虚拟机快很多,资源消耗小.这种技术主要是为了解决部署环境的 ...