Binary Tree non-recursive traverse method.

本文介绍了一种使用栈实现二叉树的前序、中序和后序非递归遍历的方法。通过记录最后弹出的节点,来确定当前栈顶节点与其的关系,从而实现正确的遍历顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值