package interview;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* None recursive pre-order, post-order and in-order.
* In order to back trace, the main idea here is using a Node that point to the last
* popped Node.
*
*
*
* The last popped Node must have some kind of relationship with the current top Node in the stack.
*
* 1. last Node is the child of top Node;
* 2. last Node is the siblings of the top Node;
* 3. last Node is the parent of the top Node;
*
*
* A. For preOrder scenario, last Node is the parent of the top Node. We can also use the same thinking
* that we never pop-up the parent unless the 2 children are visited. But here we do not need that,
* I found that the parent node is useless because when comes from right node, we certainly need to go back to the
* up level.
*
*
*
* B. For inOrder and postOrder scenario, the last Node is the child or sibling of the top Node. We never pop-up the parent unless the 2 children are visited
*
* @author wa505
*
*/
public class BinaryTreeNoneRecursive {
public static void main(String[] args) {
Stack<Node> stack = new Stack<>();
int i = 0;
Node root = new Node();
root.value = i++;
stack.push(root);
while (!stack.isEmpty() && i <= 6) {
List<Node> tempList = new ArrayList<>();
while (!stack.isEmpty()) {
Node top = stack.pop();
Node left = addLeft(top, i++);
Node right = addRight(top, i++);
tempList.add(right);
tempList.add(left);
}
for (Node node : tempList) {
stack.push(node);
}
}
BinaryTreeNoneRecursive bTree = new BinaryTreeNoneRecursive();
List<Node> preList = bTree.preOrder(root);
System.out.println("================preList");
for (Node node : preList) {
System.out.println(node.value);
}
System.out.println("================inList");
List<Node> inList = bTree.inOrder(root);
for (Node node : inList) {
System.out.println(node.value);
}
System.out.println("================postList");
List<Node> postList = bTree.postOrder(root);
for (Node node : postList) {
System.out.println(node.value);
}
}
static Node addLeft(Node node, int value) {
Node child = new Node();
child.value = value;
node.left = child;
return child;
}
static Node addRight(Node node, int value) {
Node child = new Node();
child.value = value;
node.right = child;
return child;
}
static class Node {
int value;
Node left;
Node right;
}
List<Node> preOrder(Node root) {
List<Node> result = new ArrayList<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
while (stack.size() > 0) {
Node node = stack.pop();
if (node != null) {
result.add(node);
stack.push(node.right);
stack.push(node.left);
}
}
return result;
}
List<Node> inOrder(Node root) {
List<Node> result = new ArrayList<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
Node lastNode = null;
while (stack.size() > 0) {
Node top = stack.peek();
if (top.right == lastNode && lastNode != null) {
stack.pop();
lastNode = top;
} else if (top.left == lastNode && lastNode != null) {
result.add(top);
if (top.right == null) {
stack.pop();
lastNode = top;
} else {
stack.push(top.right);
}
} else if (top.left != null) {
stack.push(top.left);
} else if (top.right != null) {
stack.push(top.right);
} else {
result.add(stack.pop());
lastNode = top;
}
}
return result;
}
List<Node> postOrder(Node root) {
List<Node> result = new ArrayList<>();
Stack<Node> stack = new Stack<>();
stack.push(root);
Node lastNode = null;
while (stack.size() > 0) {
Node top = stack.peek();
if (top.right == lastNode && lastNode != null) {
stack.pop();
lastNode = top;
result.add(top);
} else if (top.left == lastNode && lastNode != null) {
if (top.right == null) {
stack.pop();
lastNode = top;
result.add(top);
} else {
stack.push(top.right);
}
} else if (top.left != null) {
stack.push(top.left);
} else if (top.right != null) {
stack.push(top.right);
} else {
stack.pop();
lastNode = top;
result.add(top);
}
}
return result;
}
}
分享到:
相关推荐
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
标题中的"sqlite-netFx451-static-binary-bundle-x64-2013-1.0.112.0.zip"表明这是一个针对.NET Framework 4.5.1、64位(x64)平台的SQLite静态二进制捆绑包,版本号为1.0.112.0。SQLite是一款开源的关系型数据库管理...
skyeye-binary-testutils-1.0.7.tar.bz2
标题中的"amoeba-mysql-binary-2.2.0.tar" 指的是一种名为Amoeba MySQL二进制分发版的软件包,版本号为2.2.0,其存储格式为tar档案。Amoeba是一个分布式数据库中间件,它能够将一个MySQL实例透明地扩展到多个节点,...
javascript js_leetcode题解之156-binary-tree-upside-down.js
javascript js_leetcode题解之145-binary-tree-postorder-traversal.js
javascript js_leetcode题解之144-binary-tree-preorder-traversal.js
标题中的"sqlite-netFx40-static-binary-x64-2010-1.0.112.0.zip"揭示了这个压缩包是SQLite针对.NET Framework 4.0平台的静态编译版本,适用于64位操作系统。这里的“static”意味着它包含了所有必要的依赖,使得...
标题 "sqlite-netFx40-binary-x64-2010-1.0.106.0" 指的是一个针对 .NET Framework 4.0 平台的 SQLite 驱动程序的特定版本,适用于64位(x64)系统。这个版本号1.0.106.0表明这是一个更新稳定版。SQLite 是一个轻量...
skyeye-binary-testutils-1.2.0.tar.bz2
gh-ost-binary-linux-20170914095800.tar 常采用的是对几百万以上的表用pt-online-schema-change,这种方式会产生大量的binlog,业务高峰期不能做,会引起主备延迟,gh-ost有一定优势
skyeye-binary-testutils-1.0.7.1.tar.bz2
资源分类:Python库 所属语言:Python 资源全名:python-binary-memcached-0.24.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
标题中的"sqlite-netFx40-binary-Win32_2010-1.0.94.0.zip"指的是SQLite数据库的一个特定版本,适用于.NET Framework 4.0环境,面向Windows 32位操作系统,版本号为1.0.94.0。这个压缩包是为了解决在开发或运行过程...
CEF最小构建二进制库,不包含test工厂。信息如下: 1.平台:Win32 2.VS编译器版本:VS2022 3. CEF版本:115.2.3 4. Chromiun版本:115 5. 构建分支:5790 6. 编译配置:debug 和 release
gh-ost的github下载有时会及其的慢,上传1.0.49到CSDN,有需要的可以来下载。 官方下载地址: ...a1d7f72e1119bb8a939204a56acbee09eb52c769183a4649e56d6b3b524cb774 gh-ost-binary-linux-20200209110835.tar.gz
标题中的"cef_binary_88.0.0-master.2288+gd06fdcf+chromium-88.0.4324.0_windows64.zip"揭示了这个压缩包与CEF(Chromium Embedded Framework)相关,这是一个基于Google Chromium浏览器引擎的开源框架,用于在各种...
"Cifar-10-binary.tar.gz"是这个数据集的一种二进制压缩格式,相比原始的非压缩版本,它占用的存储空间更小,下载和解压也更快。在官网上下载可能速度较慢,但这个二进制压缩版可以提供一个快速的替代方案,解压后与...
CIFAR-10二进制数据集(cifar-10-binary.tar.gz)是深度学习领域常用的图像识别数据集,特别适用于训练和测试各种计算机视觉模型,如卷积神经网络(CNN)。这个数据集由Caffe框架所采用,便于进行分类任务的实践和...
javascript js_leetcode题解之124-binary-tree-maximum-path-sum.js