二叉树定义
public class ThreeNode {
public int value;
public ThreeNode rigth;
public ThreeNode left;
public ThreeNode() {
}
public ThreeNode(int value) {
super();
this.value = value;
}
public ThreeNode(int value, ThreeNode rigth, ThreeNode left) {
super();
this.value = value;
this.rigth = rigth;
this.left = left;
}
}
递归
前序
public static List<Integer> preOrderTraverse(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
List<Integer> left = preOrderTraverse(root.left);
List<Integer> right = preOrderTraverse(root.rigth);
result.add(root.value);
result.addAll(left);
result.addAll(right);
return result;
}
中序
public static List<Integer> innerOrderTraverse(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
List<Integer> left = innerOrderTraverse(root.left);
List<Integer> right = innerOrderTraverse(root.rigth);
result.addAll(left);
result.add(root.value);
result.addAll(right);
return result;
}
后序
public static List<Integer> postOrderTraverse(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
List<Integer> left = postOrderTraverse(root.left);
List<Integer> right = postOrderTraverse(root.rigth);
result.addAll(left);
result.addAll(right);
result.add(root.value);
return result;
}
非递归
前序
public static List<Integer> preOrderTraverseNot(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<ThreeNode> stack = new Stack<>();
stack.push(root);
ThreeNode current = null;
while(!stack.isEmpty()) {
current = stack.pop();
result.add(current.value);
if (current.rigth != null) {
stack.push(current.rigth);
}
if (current.left!=null) {
stack.push(current.left);
}
}
return result;
}
中序
public static List<Integer> innerOrderTraverseNot(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<ThreeNode> stack = new Stack<>();
ThreeNode current = root;
while(current !=null || !stack.isEmpty()) {
while(current != null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
result.add(current.value);
current = current.rigth;
}
return result;
}
后序
public static List<Integer> postOrderTraverseNo(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<ThreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
ThreeNode node = stack.peek();
if (node.rigth == null && node.left==null) {
result.add(node.value);
}
if (node.rigth!=null) {
stack.push(node.rigth);
node.rigth = null;
}
if (node.left != null) {
stack.push(node.left);
node.left = null;
}
}
return result;
}
public static List<Integer> postOrderTraverseNo2(ThreeNode root){
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<ThreeNode> stack = new Stack<>();
ThreeNode prev = null;
ThreeNode current = root;
stack.push(root);
while(!stack.isEmpty()) {
current = stack.peek();
if (prev == null || prev.left == current || prev.rigth == current) {
if (current.left != null) {
stack.push(current.left);
}else if(current.rigth!=null){
stack.push(current.rigth);
}
}else if (current.left == prev) {
if (current.rigth != null ) {
stack.push(current.rigth);
}
}else {
result.add(current.value);
stack.pop();
}
prev = current;
}
return result;
}