PS:文章最后附上完整代码
List<Integer> inOrder(BTree root) {
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
stack.push(root);
root = root.left;
}
node = stack.pop();
result.add(node.val);
if (null != node.right) {
root = node.right;
}
}
return result;
}
List<Integer> preOrder(BTree root) {
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
result.add(root.val);
stack.push(root);
root = root.left;
}
node = stack.pop();
if (null != node.right) {
root = node.right;
}
}
return result;
}
List<Integer> postOrder(BTree root) {
List<Integer> result = new ArrayList<>();
Stack<BTree> stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
result.add(0, root.val);
stack.push(root);
root = root.right;
}
node = stack.pop();
if (null != node.left) {
root = node.left;
}
}
return result;
}
List<Integer> layerOrder(BTree root) {
List<Integer> result = new ArrayList<>();
if (null == root) {
return result;
}
Queue<BTree> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
BTree node = queue.poll();
if (null != node.left) {
queue.offer(node.left);
}
if (null != node.right) {
queue.offer(node.right);
}
result.add(node.val);
}
return result;
}
测试代码:
import java.util.*;
/**
* Copyright © 2018 Chris. All rights reserved.
*
* @author Chris
*/
public class Solution {
static class BTree {
int val;
BTree left, right;
BTree(int v) {val = v;}
@Override
public String toString() {
return val + "";
}
}
/**
* 层次遍历
*
* @param root 根结点
* @return 遍历结果
*/
List layerOrder(BTree root) {
List result = new ArrayList<>();
if (null == root) {
return result;
}
Queue queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
BTree node = queue.poll();
if (null != node.left) {
queue.offer(node.left);
}
if (null != node.right) {
queue.offer(node.right);
}
result.add(node.val);
}
return result;
}
/**
* 后序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List postOrder(BTree root) {
List result = new ArrayList<>();
Stack stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
result.add(0, root.val);
stack.push(root);
root = root.right;
}
node = stack.pop();
if (null != node.left) {
root = node.left;
}
}
return result;
}
/**
* 前序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List preOrder(BTree root) {
List result = new ArrayList<>();
Stack stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
result.add(root.val);
stack.push(root);
root = root.left;
}
node = stack.pop();
if (null != node.right) {
root = node.right;
}
}
return result;
}
/**
* 中序遍历
*
* @param root 根结点
* @return 遍历结果
*/
List inOrder(BTree root) {
List result = new ArrayList<>();
Stack stack = new Stack<>();
BTree node;
while (null != root || !stack.empty()) {
while (null != root) {
stack.push(root);
root = root.left;
}
node = stack.pop();
result.add(node.val);
if (null != node.right) {
root = node.right;
}
}
return result;
}
public static void main(String args[]) {
Solution s = new Solution();
BTree root = new BTree(1);
BTree left = new BTree(2);
BTree right = new BTree(3);
left.left = new BTree(4);
left.right = new BTree(5);
right.left = new BTree(6);
right.right = new BTree(7);
root.left = left;
root.right = right;
System.out.println(s.inOrder(root));
System.out.println(s.preOrder(root));
System.out.println(s.postOrder(root));
System.out.println(s.layerOrder(root));
}
}