二叉树的结构
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
}
}
二叉树的前序遍历
二叉树的前序遍历----递归方式
public static void preOrderTraversal(TreeNode root) {
System.out.print(root.val + " ");
if (root.left != null) {
preOrderTraversal(root.left);
}
if (root.right != null) {
preOrderTraversal(root.right);
}
}
二叉树的前序遍历----非递归方式
public static void preOrderTraversalNoRecursion(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = null;
stack.push(root);
while (!stack.empty()) {
cur = stack.pop();
System.out.print(cur.val + " ");
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
}
二叉树的中序遍历
二叉树的中序遍历----递归方式
public static void inOrderTraversal(TreeNode root) {
if (root.left != null) {
inOrderTraversal(root.left);
}
System.out.print(root.val + " ");
if (root.right != null) {
System.out.print(root.right);
}
}
二叉树的中序遍历----非递归方式
public static void inOrderTraversalNoRecursion(TreeNode root) {
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
System.out.print(cur.val + " ");
cur = cur.right;
}
}
二叉树的后序遍历
二叉树的后序遍历----递归方式
public static void postOrderTraversal(TreeNode root) {
if (root.left != null) {
postOrderTraversal(root.left);
}
if (root.right != null) {
postOrderTraversal(root.right);
}
System.out.print(root.val + " ");
}
二叉树的后序遍历----非递归方式
public static void postOrderTraversalNoRecursion(TreeNode root) {
LinkedList<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
TreeNode rightNode = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
while (cur.right == null || cur.right == rightNode) {
System.out.print(cur.val + " ");
rightNode = cur;
if (stack.isEmpty()) {
return;
}
cur = stack.pop();
}
stack.push(cur);
cur = cur.right;
}
}
二叉树的广度优先遍历
广度优先遍历二叉树----又称层次遍历二叉树
public static void breadthFirstTraversal(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
TreeNode cur = null;
queue.offer(root);
while (!queue.isEmpty()) {
cur = queue.poll();
System.out.print(cur.val + " ");
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
Main
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
TreeNode n1 = new TreeNode(2);
TreeNode n2 = new TreeNode(1);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(6);
TreeNode n5 = new TreeNode(5);
TreeNode n6 = new TreeNode(7);
root.left = n1;
root.right = n4;
n1.left = n2;
n1.right = n3;
n4.left = n5;
n4.right = n6;
preOrderTraversal(root);
System.out.println();
preOrderTraversalNoRecursion(root);
System.out.println();
inOrderTraversal(root);
System.out.println();
inOrderTraversalNoRecursion(root);
System.out.println();
postOrderTraversal(root);
System.out.println();
postOrderTraversalNoRecursion(root);
System.out.println();
breadthFirstTraversal(root);
}
打印结果
4 2 1 3 6 5 7
4 2 1 3 6 5 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 3 2 5 7 6 4
1 3 2 5 7 6 4
4 2 6 1 3 5 7