前序遍历

 public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
list.add(p.val); // Add before going to children
p=p.left;
}else{
TreeNode node=stack.pop();
p=node.right;
}
}
return list;
}  

中序遍历

 public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
p=p.left;
}else{
TreeNode node=stack.pop();
list.add(node.val);// Add after all left children
p=node.right;
}
}
return list;
}

后序遍历

public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> list = new LinkedList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode p=root;
while(!stack.isEmpty()||p!=null){
if(p!=null){
stack.push(p);
list.addFirst(p.val);// Reverse the process of preorder
p=p.right;
}else{
TreeNode node=stack.pop();
p=node.left;
}
}
return list;
}

  

  

【LeetCode】二叉搜索树的前序,中序,后续遍历非递归方法的更多相关文章

  1. 剑指 Offer 36. 二叉搜索树与双向链表 + 中序遍历 + 二叉排序树

    剑指 Offer 36. 二叉搜索树与双向链表 Offer_36 题目描述 题解分析 本题考查的是二叉树的中序遍历以及二叉排序树的特征(二叉排序树的中序遍历序列是升序序列) 利用排序二叉树中序遍历的性 ...

  2. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  3. [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...

  4. [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...

  5. [LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...

  6. [程序员代码面试指南]二叉树问题-在二叉树中找到两个节点的最近公共祖先、[LeetCode]235. 二叉搜索树的最近公共祖先(BST)(非递归)

    题目 题解 法一: 按照递归的思维去想: 递归终止条件 递归 返回值 1 如果p.q都不在root为根节点的子树中,返回null 2 如果p.q其中之一在root为根节点的子树中,返回该节点 3 如果 ...

  7. 剑指offer-面试题36-二叉搜索树与双向链表-中序遍历

    /* 题目: 将二叉搜索树转化为排序的双向链表,不能创建新的节点, 只能调整节点的指向,返回双向链表的头节点. */ /* 思路: 递归. 二叉搜索树的中序遍历得到的序列是递增序列. 左子树left& ...

  8. Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作

    1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...

  9. [Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...

随机推荐

  1. 英语口语练习系列-C24-杂技-问候语-乡愁

    1. 词汇-杂技 acrobatics noun [ U ] UK /ˌæk.rəˈbæt.ɪks/ US /ˌæk.rəˈbæt̬.ɪks/ the skills of an acrobat 杂技 ...

  2. (数据存储)Android系统存储数据

    移动设备需要存储数据,处理数据并输出处理后的信息. 主题一:存储键值对 If you have a relatively small collection of key-values that you ...

  3. 免花生壳 TCP测试 DTU测试 GPRS测试TCP服务器

    通常在学习GPRS或者DTU的时候,往往没有自己的服务器,很多时候我们只能用这个模块打个电话发个短信,但是随着移动互联的兴起,各行各业大家都开始弄移动接入.为了这个需求,这里提供TCP移动接入. 工作 ...

  4. 轻松掌握Redux-Action使用方法

    轻松掌握Redux-Action使用方法 Redux-Action主要有两个方法,createAction和createAction,只要掌握了这两个方法就会了redux-action的使用. cre ...

  5. Scrapy 使用CrawlSpider整站抓取文章内容实现

    刚接触Scrapy框架,不是很熟悉,之前用webdriver+selenium实现过头条的抓取,但是感觉对于整站抓取,之前的这种用无GUI的浏览器方式,效率不够高,所以尝试用CrawlSpider来实 ...

  6. [P1441]砝码称重 (搜索+DP)

    对于我这种蒟蒻,是很不错的一题了. dfs搜索当前状态 满足时DP 比较坑的地方就是起始的地方 我一开始从1开始,搜索写的是从0开始. 后来就统一用0开始的了. #include<bits/st ...

  7. meta中minimal-ui属性

    <meta id="viewport" name="viewport" content="width=device-width, user-sc ...

  8. Python基础-列表、元祖、字典、字符串

    列表和分组 序列概览: 数据结构是通过某种方式组织在一起的数据元素的集合.这些元素可以是数字.字符,甚至可以是其他数据结构. 在python中,最基本的数据结构是序列(sequence). 序列中的每 ...

  9. mysql 命令语句

    1.大部分数据库命令语句 数据库的命令 net start MYsql 启动的服务 net stop mysql 关闭服务 mysql -uroot -p 输入数据库名和密码登入 show datab ...

  10. Docker 常用命令(一)

    9. docker 删除镜像:  docker rmi    imageID    删除容器:    docker rm containName 8. docker repo 上传: 我们看到这里有个 ...