You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.2 KiB
73 lines
1.2 KiB
package class10;
|
|
|
|
public class Code02_RecursiveTraversalBT {
|
|
|
|
public static class Node {
|
|
public int value;
|
|
public Node left;
|
|
public Node right;
|
|
|
|
public Node(int v) {
|
|
value = v;
|
|
}
|
|
}
|
|
|
|
public static void f(Node head) {
|
|
if (head == null) {
|
|
return;
|
|
}
|
|
// 1
|
|
f(head.left);
|
|
// 2
|
|
f(head.right);
|
|
// 3
|
|
}
|
|
|
|
// 先序打印所有节点
|
|
public static void pre(Node head) {
|
|
if (head == null) {
|
|
return;
|
|
}
|
|
System.out.println(head.value);
|
|
pre(head.left);
|
|
pre(head.right);
|
|
}
|
|
|
|
public static void in(Node head) {
|
|
if (head == null) {
|
|
return;
|
|
}
|
|
in(head.left);
|
|
System.out.println(head.value);
|
|
in(head.right);
|
|
}
|
|
|
|
public static void pos(Node head) {
|
|
if (head == null) {
|
|
return;
|
|
}
|
|
pos(head.left);
|
|
pos(head.right);
|
|
System.out.println(head.value);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Node head = new Node(1);
|
|
head.left = new Node(2);
|
|
head.right = new Node(3);
|
|
head.left.left = new Node(4);
|
|
head.left.right = new Node(5);
|
|
head.right.left = new Node(6);
|
|
head.right.right = new Node(7);
|
|
|
|
pre(head);
|
|
System.out.println("========");
|
|
in(head);
|
|
System.out.println("========");
|
|
pos(head);
|
|
System.out.println("========");
|
|
|
|
}
|
|
|
|
}
|