递归实现:
import java.util.LinkedList;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public class Solution {
private TreeNode createBinaryTree() {
TreeNode root = new TreeNode(0);
root.left = new TreeNode(1);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right = new TreeNode(2);
root.right.right = new TreeNode(5);
return root;
}
public static void main(String[] args) {
Solution test = new Solution();
TreeNode root = test.createBinaryTree();
test.preOrder(root);
System.out.println("\n=============================================");
test.midOrder(root);
System.out.println("\n=============================================");
test.postOrder(root);
System.out.println("\n=============================================");
test.levelOrder(root);
System.out.println("\n=============================================");
}
// 前序遍历
private void preOrder(TreeNode root) {
if(root == null) return;
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
// 中序遍历
private void midOrder(TreeNode root) {
if(root == null) return;
midOrder(root.left);
System.out.print(root.val + " ");
midOrder(root.right);
}
// 后序遍历
private void postOrder(TreeNode root) {
if(root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val + " ");
}
// 层序遍历
private void levelOrder(TreeNode root) {
if(root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode t = queue.poll();
if(t.left != null) queue.offer(t.left);
if(t.right != null) queue.offer(t.right);
System.out.print(t.val + " ");
}
}
}
输出:
非递归实现:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
public class Solution {
private TreeNode createBinaryTree() {
TreeNode root = new TreeNode(0);
root.left = new TreeNode(1);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right = new TreeNode(2);
root.right.right = new TreeNode(5);
return root;
}
public static void main(String[] args) {
Solution test = new Solution();
TreeNode root = test.createBinaryTree();
test.preOrder(root);
System.out.println("\n=============================================");
test.midOrder(root);
System.out.println("\n=============================================");
test.postOrder(root);
System.out.println("\n=============================================");
test.levelOrder(root);
System.out.println("\n=============================================");
}
// 前序遍历
private void preOrder(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
System.out.print(root.val + " ");
root = root.left;
}
if(!stack.isEmpty()) {
root = stack.pop();
root = root.right;
}
}
}
// 中序遍历
private void midOrder(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()) {
root = stack.pop();
System.out.print(root.val + " ");
root = root.right;
}
}
}
// 后序遍历
private void postOrder(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
Stack<Integer> ans = new Stack<>();
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
ans.push(root.val);
root = root.right;
}
if(!stack.isEmpty()) {
root = stack.pop();
root = root.left;
}
}
while(!ans.isEmpty()) {
System.out.print(ans.pop() + " ");
}
}
// 层序遍历
private void levelOrder(TreeNode root) {
if(root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
TreeNode t = queue.poll();
if(t.left != null) queue.offer(t.left);
if(t.right != null) queue.offer(t.right);
System.out.print(t.val + " ");
}
}
}
输出:
能力有限,未写推理过程,觉得这篇文章写的很不错,有兴趣的朋友可以去了解下:https://blog.csdn.net/zhangxiangdavaid/article/details/37115355