forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraverseBinaryTree.java
82 lines (76 loc) · 1.98 KB
/
TraverseBinaryTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cn.codepub.algorithms.trees;
import cn.codepub.algorithms.utils.Tree;
/**
* <p>
* Created with IntelliJ IDEA. 2015/10/30 14:16
* </p>
* <p>
* ClassName:TraverseTree
* </p>
* <p>
* Description:提供对二叉树的三种遍历方式:先序遍历,中序遍历,后序遍历
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class TraverseBinaryTree {
public static void main(String[] args) {
Tree root = new Tree(1);
Tree l1 = new Tree(3);
Tree l2 = new Tree(9);
Tree l3 = new Tree(11);
Tree r1 = new Tree(5);
Tree r2 = new Tree(12);
Tree r3 = new Tree(13);
root.left = l1;
root.right = r1;
l1.left = l2;
l2.left = l3;
l3.right = r2;
r2.right = r3;
System.out.println("先序遍历");
preOrder(root);
System.out.println("\n中序遍历");
inOrder(root);
System.out.println("\n后序遍历");
postOrder(root);
}
public static void preOrder(Tree root) {
if (root == null) {
return;
}
System.out.print(root.value + "\t");
if (root.left != null) {
preOrder(root.left);
}
if (root.right != null) {
preOrder(root.right);
}
}
public static void inOrder(Tree root) {
if (root == null) {
return;
}
if (root.left != null) {
inOrder(root.left);
}
System.out.print(root.value + "\t");
if (root.right != null) {
inOrder(root.right);
}
}
public static void postOrder(Tree root) {
if (root == null) {
return;
}
if (root.left != null) {
postOrder(root.left);
}
if (root.right != null) {
postOrder(root.right);
}
System.out.print(root.value + "\t");
}
}