二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal
前序的非递归遍历:用堆来实现
如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印
如果用队列替代堆,并且先存储左节点,再存储右节点,就变成了逐行打印
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
if(root == NULL)
return result;
stack<TreeNode*> sta;
sta.push(root);
while(!sta.empty()){
TreeNode* node = sta.top();
sta.pop();
result.push_back(node->val);
if(node->right)
sta.push(node->right);
if(node->left)
sta.push(node->left);
}
return result;
}
};
94. Binary Tree Inorder Traversal
思路:用一个变量node记录当前节点,每次先遍历并存储所有的左节点直到为空,然后栈里顶部存储的那个节点就是最近父节点,然后再去遍历一个右节点,在右节点中继续寻找左节点
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> container;
TreeNode* cur = root;
while(cur || !container.empty()){
while(cur){
container.push(cur);
cur = cur->left;
}
cur = container.top();
container.pop();
result.push_back(cur->val);
cur = cur->right;
}
return result;
}
};
145. Binary Tree Postorder Traversal
http://www.cnblogs.com/grandyang/p/4251757.html的第二种方法
思路:前序遍历是中->左->右,后序遍历是左->右->中。在前序遍历的基础上,insert到begin的方式相当于把顺序全调换了,即变成了右->左->中,这个时候只需要再调换依稀右和左就能变成后序遍历的顺序。
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
if(root == NULL)
return result;
stack<TreeNode*> sta;
sta.push(root);
while(!sta.empty()){
TreeNode* node = sta.top();
sta.pop();
result.insert(result.begin(),node->val);
if(node->left)
sta.push(node->left);
if(node->right)
sta.push(node->right);
}
return result;
}
};
173. Binary Search Tree Iterator
这个题其实就是中序非递归遍历,并且就是另一种写法的实现,也就是先在循环外将左节点全部装入stack
注意:初始化只是将第一次所有的左节点装入stack,next是一次一次的递归
class BSTIterator {
public:
BSTIterator(TreeNode* root) {
while(root){
sta.push(root);
root = root->left;
}
}
/** @return the next smallest number */
int next() {
TreeNode* node = sta.top();
sta.pop();
TreeNode* root = node->right;
while(root){
sta.push(root);
root = root->left;
}
return node->val;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !sta.empty();
}
stack<TreeNode*> sta;
};
二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历
题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- URAL 1136 Parliament 二叉树水题 BST后序遍历建树
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
随机推荐
- url字符转义
作者在做短链接功能时,url参数里带了&字符,结果无法转换.后来查了一下,发现可以用其它符号代替.下面是对应表 + URL 中+号表示空格 ...
- No.1一步步学习vuejs 环境配置安装篇
一 安装与配置 需要安装node.js和 nmp管理器http://nodejs.cn/安装完后测试输入命令查看版本验证 node -v //查看node.js的版本 npm -v //查看 由于有些 ...
- redis的安全问题
1.修改redis.conf配置文件 2.重启redis服务,使其生效 3.成功登陆以后,使用auth+密码 或者在登录的时候使用-a 密码的授权方式
- 移动端 line-height 不垂直居中问题
本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 一般情况下, 我们把 line-height 的值设置为 height 的 ...
- windows定位dll的搜索顺序
原文:http://blog.csdn.net/zzxian/article/details/6429293 Visual C++ Windows 用来定位 DLL 的搜索路径 通过隐式和显式链接,W ...
- 【Udacity】线性回归方程 Regression
Concept in English Coding Portion 评估回归的性能指标--R平方指标 比较分类和回归 Continuous supervised learning 连续变量监督学习 R ...
- ES6入门——let和const命令
let和const命令 1.let命令 用法:类似于var,用来声明一个变量,区别是所声明的变量只在let命令所在的代码块内有效. let命令很适合用在for循环的计数器中,因为let声明的变量仅在作 ...
- 3.获取git仓库
有两种取得 Git 项目仓库的方法. 第一种是在现有项目或目录下导入所有文件到 Git 中: 第二种是从一个服务器克隆一个现有的 Git 仓库. 在现有目录中初始化仓库 如果你打算使用 Git 来对现 ...
- 自动下载和安装 MNIST 到 TensorFlow 的 python 源码 (转)
# Copyright 2015 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 ...
- 关于实现XX系统设计时所实现的质量属性战术
可用性: 1)使用Try-catch对抛出的异常进行处理 2)使用Spring事务管理 易用性: 1)在类似删除相关选项时,弹出提示框,防止误操作 2)在不编辑基本信息时,对其进行折叠或者隐藏 3)提 ...