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] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后 ...
 
随机推荐
- 使用servlet实现用户注册功能
			
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
 - hdu3847Trash Removal(凸包)
			
链接 这题居然是WF的题, 应属于签到题.. 求一个多边形是否能被一个宽为d的矩形框住. 可以求一下凸包,然后枚举每条凸包的边,找出距离最远的点. #include <iostream> ...
 - 做个这样的APP要多久?[转]
			
这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...
 - ASP.NET MVC Html.Partial/Html.RenderPartial/Html.Action/Html.RenderAction区别
			
1. @Html.Raw() 方法输出带有html标签的字符串: <div style="margin:10px 0px 0px;border:1px;border-color:red ...
 - html中button自动提交表单?
			
在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit
 - 并发编程 04——闭锁CountDownLatch 与 栅栏CyclicBarrier
			
Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...
 - Question and Answer
			
1.VS2013使用EntityFrame问题解决办法 解决办法参照博客http://pinter.org/?p=2374 使用到EntityFrame的项目配置文件修改如下: 项目中凡是使用到DbC ...
 - 编写HTML表单提交并接受数据信息(获取用户通过表单提交的内容)
			
MyInfoForm.java package com.fxl; import java.io.IOException; import java.io.PrintWriter; import java ...
 - iOS开发UI篇—ios应用数据存储方式(偏好设置)
			
iOS开发UI篇—ios应用数据存储方式(偏好设置) 一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用 ...
 - DeepLearning之路(二)SoftMax回归
			
Softmax回归 1. softmax回归模型 softmax回归模型是logistic回归模型在多分类问题上的扩展(logistic回归解决的是二分类问题). 对于训练集,有. 对于给定的测试 ...