-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSolution08.java
More file actions
76 lines (70 loc) · 1.76 KB
/
Copy pathSolution08.java
File metadata and controls
76 lines (70 loc) · 1.76 KB
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
package sword_offer;
//page 65 二叉树的下一个节点
//定义二叉树类,包含左右子树、父节点,以及用于查看的函数
class TreeNode {
char value;
TreeNode left;
TreeNode right;
TreeNode father;
//中序遍历输出查看
public void printList() {
if (left != null)
left.printList();
System.out.println(value);
if (right != null)
right.printList();
}
}
public class Solution08 {
public static TreeNode findNext(TreeNode node) {
//有右节点,找到右子树的最左节点
if (node.right!= null) {
node = node.right;
while(node.left != null) node = node.left;
return node;
}
//无右节点,则向上遍历,直至找到节点为父节点的左子节点
while(node.father != null) {
if (node.father.left == node) return node.father;
node = node.father;
}
return null;
}
public static void main(String[] args) {
//建立一个二叉树节点的数组,并tree[i].value赋值
TreeNode[] tree = new TreeNode[9];
char[] chars = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
for (int i = 0; i < tree.length; i++) {
tree[i] = new TreeNode();
tree[i].value = chars[i];
}
/*
* a
* / \
* b c
* / \ / \
* d e f g
* / \
* h i
*/
//左右节点关系
tree[0].left = tree[1];
tree[0].right = tree[2];
tree[1].left = tree[3];
tree[1].right = tree[4];
tree[2].left = tree[5];
tree[2].right = tree[6];
tree[4].left = tree[7];
tree[4].right = tree[8];
//父节点关系
tree[1].father = tree[0];
tree[2].father = tree[0];
tree[3].father = tree[1];
tree[4].father = tree[1];
tree[5].father = tree[2];
tree[6].father = tree[2];
tree[7].father = tree[4];
tree[8].father = tree[4];
tree[0].printList();
}
}