-
Notifications
You must be signed in to change notification settings - Fork 14
/
Array2MaxTree.java
127 lines (105 loc) · 2.55 KB
/
Array2MaxTree.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
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
package com.demo;
import java.util.HashMap;
import java.util.Stack;
/**
* 构造数组的maxTree
* MaxTree定义:
* 1.数组没有重复数字
* 2.MaxTree是一颗二叉树,每个节点对应数组的一个元素
* 3.每一颗树上,最大的值都在头节点
* @author kexun
*
*/
public class Array2MaxTree {
public static void main(String[] args) {
int[] a = {
5,7,1,6,4,8,9
};
Array2MaxTree m = new Array2MaxTree();
Node tree = m.getMaxTree(a);
System.out.println(tree.value);
System.out.println(tree.left.value);
System.out.println(tree.right.value);
}
class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
}
}
public Node getMaxTree(int[] array) {
int length = array.length;
if (length == 0) {
return null;
}
if (length == 1) {
return new Node(array[0]);
}
Node[] nodes = new Node[length];
for (int i=0; i<length; i++) {
nodes[i] = new Node(array[i]);
}
Stack<Node> stack = new Stack<Node>();
HashMap<Node, Node> lmap = new HashMap<Node, Node>();
HashMap<Node, Node> rmap = new HashMap<Node, Node>();
for (int i=0; i<length; i++) {
Node currNode = nodes[i];
while (!stack.isEmpty() && stack.peek().value < currNode.value) {
popStack2Map(stack, lmap);
}
stack.push(currNode);
}
while (!stack.isEmpty()) {
popStack2Map(stack, lmap);
}
for (int i=length-1; i>=0; i--) {
Node currNode = nodes[i];
while (!stack.isEmpty() && stack.peek().value < currNode.value) {
popStack2Map(stack, rmap);
}
stack.push(currNode);
}
while (!stack.isEmpty()) {
popStack2Map(stack, rmap);
}
Node head = null;
for (int i=0; i<length; i++) {
Node currNode = nodes[i];
Node left = lmap.get(currNode);
Node right = rmap.get(currNode);
if (left == null && right == null) {
head = currNode;
} else if (left == null) {
if (right.left == null) {
right.left = currNode;
} else {
right.right = currNode;
}
} else if (right == null) {
if (left.left == null) {
left.left = currNode;
} else {
left.right = currNode;
}
} else {
Node parent = left.value < right.value ? left : right;
if (parent.left == null) {
parent.left = currNode;
} else {
parent.right = currNode;
}
}
}
return head;
}
public void popStack2Map(Stack<Node> stack, HashMap<Node, Node> lmap) {
Node temp = stack.pop();
if (stack.isEmpty()) {
lmap.put(temp, null);
} else {
lmap.put(temp, stack.peek());
}
}
}