## 复习数据结构 原

千里山南

``````public class Test_01 {
static class Node {
Node parent;
Node left;
Node right;
String content;
boolean visited;
Node(String content) {
this.content = content;
}

void setLeft(Node left) {
this.left = left;
left.parent = this;
}

void setRight(Node right) {
this.right = right;
right.parent = this;
}
}

public static void main(String[] args) {
Node na = new Node("A");
Node nb = new Node("B");
Node nc = new Node("C");
Node nd = new Node("D");
Node ne = new Node("E");
Node nf = new Node("F");
Node ng = new Node("G");
Node nh = new Node("H");
Node ni = new Node("I");

na.setLeft(nb);
na.setRight(nc);

nb.setLeft(nd);
nb.setRight(ne);

nc.setLeft(nf);
nc.setRight(ng);

ne.setLeft(ni);
nf.setLeft(nh);

test1(na);
System.out.println();
System.out.println("==========================");
test2(na);
System.out.println();
System.out.println("==========================");
test3(na);
System.out.println();
System.out.println("==========================");
test4(na);
System.out.println();
System.out.println("==========================");
test5(na);
System.out.println();
System.out.println("==========================");
test6(na);
}

private static void test1(Node root) {
if (root != null) {
System.out.print(root.content+",");
test1(root.left);
test1(root.right);
}
}

private static void test2(Node root) {
if (root != null) {
test2(root.left);
System.out.print(root.content+",");
test2(root.right);
}
}

private static void test3(Node root) {
if (root != null) {
test3(root.left);
test3(root.right);
System.out.print(root.content+",");
}
}

private static void test4(Node root) {
Stack<Node> stack = new Stack<Node>();
Node current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}
current = stack.peek();
stack.pop();
System.out.print(current.content+",");
current = current.right;
}
}

private static void test5(Node root) {
Stack<Node> stack = new Stack<Node>();
Node current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
System.out.print(current.content+",");
stack.push(current);
current = current.left;
}
current = stack.peek();
stack.pop();
current = current.right;
}
}

private static void test6(Node root) {
Stack<Node> stack = new Stack<Node>();
Node current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}
current = stack.peek();
stack.pop();
if (!current.visited) {
current.visited = true;
stack.push(current);
current = current.right;
} else {
System.out.print(current.content+",");
current = null;
}
}
}
}
``````

### 千里山南

PHP 源码阅读日记

2016/03/30
22
0

2016/01/02
115
0

20111564
2014/10/16
0
0

waikei
2015/09/18
4.4K
57

2014/03/05
163
4

31分钟前
0
0
Python 打开目录与指定文件

Python打开外部文件有很多方法， os.popen打开外部程序，但发现只能打开文件所在目录的文件 os.system可打开外部文件os.system(command) command 要执行的命令，相当于在Windows...

shzwork
33分钟前
2
0
Leetcode # 118:Pascal's Triangle 杨辉三角

118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. 给定一个非负整数 *numRows，*生成杨辉三角的前 numRows 行。 ......

iCodeBugs
43分钟前
1
0

1.File > Open 找到项目后选择build.gradle文件，点击ok image 2.点击Open as Project image 3.选择本地Gradle以及JDK image 4.点OK完成...

48分钟前
2
0
Python实现斐波那契数列

49分钟前
3
0