Skip to content

Commit 7d353ea

Browse files
advance tree questions
1 parent d623a5e commit 7d353ea

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Main {
2+
public static void main(String[] args) {
3+
4+
}
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class PreIn {
2+
3+
public TreeNode buildTree(int[] preOrder, int[] inOrder) {
4+
HashMap<Integer, Integer> map = new HashMap<>();
5+
6+
for(int i=0; i < inOrder.length; i++) {
7+
map.put(inOrder[i], i);
8+
}
9+
10+
int[] index = {0};
11+
12+
return helper(preOrder, inOrder, 0, preOrder.length-1, map, index);
13+
}
14+
15+
public TreeNode helper(int[] preOrder, int[] inOrder, int left, int right, HashMap<Integer, Integer> map, int[] index) {
16+
if (left > right) {
17+
return null;
18+
}
19+
20+
int current = preOrder[index[0]];
21+
index[0]++;
22+
23+
TreeNode node = new TreeNode(current);
24+
25+
if (left == right) {
26+
return node;
27+
}
28+
29+
node.left = helper(preOrder, inOrder, left, index - 1, map, index);
30+
node.right = helper(preOrder, inOrder, index + 1, right, map, index);
31+
32+
return node;
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class VerticalTraversal {
2+
public List<List<Integer>> verticalTraversal(TreeNode node) {
3+
List<List<Integer>> ans = new ArrayList<List<Integer>>();
4+
5+
if (node == null) {
6+
return ans;
7+
}
8+
9+
int col = 0;
10+
11+
Queue<Map.Entry<TreeNode, Integer>> queue = new ArrayDeque<>();
12+
Map<Integer, ArrayList<Integer>> map = new HashMap();
13+
14+
queue.offer(new AbstractMap.SimpleEntry<>(node, col));
15+
16+
int min = 0;
17+
int max = 0;
18+
19+
while(!queue.isEmpty()) {
20+
Map.Entry<TreeNode, Integer> removed = queue.poll();
21+
node = removed.getKey();
22+
col = removed.getValue();
23+
24+
if(node != null) {
25+
if(!map.containsKey(col)) {
26+
map.put(col, new ArrayList<Integer>());
27+
}
28+
29+
map.get(col).add(node.val);
30+
31+
min = Math.min(min, col);
32+
max = Math.max(max, col);
33+
34+
queue.offer(new AbstractMap.SimpleEntry<>(node.left, col-1));
35+
queue.offer(new AbstractMap.SimpleEntry<>(node.right, col+1));
36+
}
37+
}
38+
39+
for(int i=min; i <= max; i++) {
40+
ans.add(map.get(i));
41+
}
42+
43+
return ans;
44+
}
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class WordLadder {
2+
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
3+
if (!wordList.contains(endWord))
4+
return 0;
5+
6+
Set<String> visited = new HashSet<>();
7+
Queue<String> q = new LinkedList<>();
8+
q.offer(beginWord);
9+
int length = 0;
10+
11+
while (!q.isEmpty()) {
12+
int size = q.size();
13+
length++;
14+
15+
for (int i = 0; i < size; i++) {
16+
String current = q.poll();
17+
18+
for (int j = 0; j < current.length(); j++) {
19+
char[] temp = current.toCharArray();
20+
21+
for (char c = 'a'; c <= 'z'; c++) {
22+
temp[j] = c;
23+
String newWord = new String(temp);
24+
25+
if (newWord.equals(endWord))
26+
return length + 1;
27+
28+
if (wordList.contains(newWord) && !visited.contains(newWord)) {
29+
q.offer(newWord);
30+
visited.add(newWord);
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
return 0;
38+
}
39+
}

0 commit comments

Comments
 (0)