leetcode(144,94,145,102)中迭代版的二叉树的前、中、后、层级遍历
#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)中迭代版的二叉树的前、中、后、层级遍历的更多相关文章
- leetcode 105 106 从前序与中序遍历序列构造二叉树 从中序与后序遍历序列构造二叉树
题目: 105 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = ...
- java实现二叉树的前中后遍历(递归和非递归)
这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- Java 非递归实现 二叉树的前中后遍历以及层级遍历
class MyBinaryTree<T> { BinaryNode<T> root; public MyBinaryTree() { root = new BinaryNod ...
- Binary Tree Traversal 二叉树的前中后序遍历
[抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树
出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...
- LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal
题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...
随机推荐
- WordPress搬家教程:换空间与换域名
WordPress搬家教程:换空间与换域名 由于本人博客空间8月份已到期,便新购一个虚拟主机想进行WordPress搬家,于是特意在网上查了些WordPress搬家教程,进行了综合总结,并结合这次实操 ...
- 深入浅出设计模式——命令模式(Command Pattern)
模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请 ...
- 原生cookie
出于浏览器的安全性限制,从WEB应用程序中访问用户本地文件系统是有许多限制的.但是WEB站点的开发人员可以使用cookie,将少量信息保存在用户本地硬盘的指定空间中. document对象的cooki ...
- python uuid、hex study
由 import uuid product[“SourceInfo"]["ProductID"] = uuid.uuid4().hex 引起的uuid 一.概述 uuid ...
- UiAutomator环境搭建及详细操作
一.环境搭建 1.1 必备条件 JDK SDK(API高于15) Eclipse(安装ADT插件) ANT(用于编译生成的jar) 安装JDK并添加环境变量 1.2 详细步骤 1.安装JDK并添加环境 ...
- MFC编程入门之十一(对话框:模态对话框及其弹出过程)
加法计算器对话框程序大家照着做一遍后,相信对基于对话框的程序有了些解了,有个好的开始对于以后的学习大有裨益.趁热打铁,这一节讲讲什么是对话框和非模态对话框,以及模态对话框怎样弹出. 一.模态对话框和非 ...
- json 后台传前台
jsonObject需实例化new.jsonObject=new JSONObject();jsonObject.put("goodslist", list);jsonObject ...
- Openvpn 本地密码验证
1.修改配置文件.(添加下列配置) auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #开启用户密码脚本 client-cert-not-r ...
- high-frequency words and phases
abide by(=be faithful to ; obey vt)忠于:遵守. a) Plese feel assured we will abide by our promise. 2. be ...
- 【转】selenium之 定位以及切换frame
转载自:http://www.voidcn.com/blog/huilan_same/article/p-6155896.html 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明 ...