-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryTree2.java
More file actions
184 lines (168 loc) · 3.82 KB
/
BinaryTree2.java
File metadata and controls
184 lines (168 loc) · 3.82 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package binarytree;
import java.util.ArrayList;
public class BinaryTree2 {
//计算二叉树距离辅助类
public static class Value {
int depth;
int distance;
}
//判断平衡二叉树辅助类
public static class BalanceValue {
boolean isBalance;
int treeHeight;
}
//插入结点
static void insertNode(Tree tree, int i) {
if (tree == null) {
return;
}
Node tNode = tree.root;
Node pNode = null;
Node nNode = new Node();
nNode.value = i;
while (tNode != null) {
pNode = tNode;
if (tNode.value <= i) {
tNode = tNode.right;
} else {
tNode = tNode.left;
}
}
if (pNode == null) {
tree.root = nNode;
} else if (pNode.value > i) {
pNode.left = nNode;
} else {
pNode.right = nNode;
}
tree.size++;
}
//生成树
static Tree createTree(int[] is) {
Tree tree = new Tree();
tree.size = 0;
for (int i = 0; i < is.length; i++) {
insertNode(tree, is[i]);
}
return tree;
}
//中序遍历
public static void PrintInorder(Node node) {
if (node == null) {
return;
}
PrintInorder(node.left);
System.out.print(node.value);
PrintInorder(node.right);
}
//镜像二叉树
public static void swapTree(Node node) {
if (node == null) {
return;
}
Node temp = node.left;
node.left = node.right;
node.right = temp;
swapTree(node.left);
swapTree(node.right);
}
//判断二叉树中元素存在
public static boolean isInTree(Node node, int n) {
if (node == null) {
return false;
}
if (node.value == n) {
return true;
}
boolean is = isInTree(node.left, n);
if (!is) {
is = isInTree(node.right, n);
}
return is;
}
//二叉树中和为s的路径
public static void printSum(ArrayList<Node> nodes, Node node, int sum) {
if (node == null) {
return;
}
nodes.add(node);
boolean isLeaf = node.left == null && node.right == null;
if (node.value == sum && isLeaf) {
for (int i = 0; i < nodes.size(); i++) {
System.out.println(nodes.get(i).value);
}
}
printSum(nodes, node.left, sum - node.value);
printSum(nodes, node.right, sum - node.value);
nodes.remove(nodes.size() - 1);
}
//二叉树最大距离
public static Value calculateMaxTreeDistance(Node node) {
Value value = new Value();
if (node == null) {
return null;
}
if (node.left == null && node.right == null) {
value.depth = 0;
value.distance = 0;
return value;
}
int leftDepth = -1;
int leftDistance = 0;
if (node.left != null) {
Value left = calculateMaxTreeDistance(node.left);
leftDepth = left.depth;
leftDistance = left.distance;
}
int rightDepth = -1;
int rightDistance = 0;
if (node.right != null) {
Value right = calculateMaxTreeDistance(node.right);
rightDepth = right.depth;
rightDistance = right.distance;
}
if (leftDepth > rightDepth) {
value.depth = leftDepth + 1;
} else {
value.depth = rightDepth + 1;
}
int through = leftDepth + rightDepth + 2;
int notThrough;
if (leftDistance > rightDistance) {
notThrough = leftDistance;
} else {
notThrough = rightDistance;
}
if (through > notThrough) {
value.distance = through;
} else {
value.distance = notThrough;
}
return value;
}
//判断是否二叉树
public static BalanceValue isTreeBalance(Node node) {
BalanceValue bValue = new BalanceValue();
if (node == null) {
bValue.isBalance = true;
bValue.treeHeight = -1;
return bValue;
}
BalanceValue left = isTreeBalance(node.left);
if (left.isBalance) {
BalanceValue right = isTreeBalance(node.right);
if (right.isBalance) {
if (left.treeHeight - right.treeHeight == 0 || left.treeHeight - right.treeHeight == -1) {
bValue.isBalance = true;
bValue.treeHeight = right.treeHeight + 1;
} else if (left.treeHeight - right.treeHeight == 1) {
bValue.isBalance = true;
bValue.treeHeight = left.treeHeight + 1;
}
return bValue;
}
}
bValue.isBalance = false;
return bValue;
}
}