Skip to content

Commit a4f60ed

Browse files
committed
Added 3 solutions
1 parent f2eddae commit a4f60ed

File tree

3 files changed

+107
-15
lines changed

3 files changed

+107
-15
lines changed

Easy/Two Sum IV - Input is a BST.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,42 @@
88
* }
99
*/
1010
class Solution {
11-
Set<Integer> set = new HashSet<>();
1211
public boolean findTarget(TreeNode root, int k) {
13-
if (root == null) return false;
14-
if (set.contains(k - root.val)) {
15-
return true;
12+
Stack<TreeNode> stack = new Stack<>();
13+
while (root != null) {
14+
stack.push(root);
15+
root = root.left;
1616
}
1717

18-
set.add(root.val);
18+
Set<Integer> set = new HashSet<>();
19+
int min = popStack(stack);
20+
set.add(min);
1921

20-
return findTarget(root.right, k) || findTarget(root.left, k);
22+
while (!stack.isEmpty()) {
23+
int num = popStack(stack);
24+
if (set.contains(k-num)) {
25+
return true;
26+
}
27+
28+
if (min + num > k) {
29+
return false;
30+
}
31+
32+
set.add(num);
33+
}
34+
35+
return false;
36+
}
37+
38+
private int popStack(Stack<TreeNode> stack) {
39+
TreeNode n = stack.pop();
40+
TreeNode right = n.right;
41+
42+
while (right != null) {
43+
stack.push(right);
44+
right = right.left;
45+
}
46+
47+
return n.val;
2148
}
2249
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
Map<Integer, List<Integer>> map = new TreeMap<>();
12+
public List<List<Integer>> verticalOrder(TreeNode root) {
13+
List<List<Integer>> ans = new ArrayList<>();
14+
updateMap(root, 0);
15+
16+
for (int key : map.keySet()) {
17+
ans.add(map.get(key));
18+
}
19+
20+
return ans;
21+
}
22+
23+
private void updateMap(TreeNode root, int level) {
24+
if (root == null) {
25+
return;
26+
}
27+
28+
Queue<Element> queue = new LinkedList<>();
29+
queue.add(new Element(root, 0));
30+
31+
while (!queue.isEmpty()) {
32+
int size = queue.size();
33+
34+
while (size-- > 0) {
35+
Element temp = queue.remove();
36+
if (map.containsKey(temp.level)) {
37+
map.get(temp.level).add(temp.node.val);
38+
}
39+
else {
40+
List<Integer> list = new ArrayList<>();
41+
list.add(temp.node.val);
42+
map.put(temp.level, list);
43+
}
44+
45+
if (temp.node.left != null) {
46+
queue.add(new Element(temp.node.left, temp.level-1));
47+
}
48+
49+
if (temp.node.right != null) {
50+
queue.add(new Element(temp.node.right, temp.level+1));
51+
}
52+
}
53+
}
54+
}
55+
}
56+
57+
class Element {
58+
public TreeNode node;
59+
public int level;
60+
61+
public Element(TreeNode node, int level) {
62+
this.node = node;
63+
this.level = level;
64+
}
65+
}

Medium/Maximum Binary Tree.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@
99
*/
1010
class Solution {
1111
public TreeNode constructMaximumBinaryTree(int[] nums) {
12-
if (nums == null) return null;
13-
return constructMaximumBinaryTreeImpl(nums,0,nums.length-1);
12+
if (nums.length == 0) {
13+
return null;
14+
}
15+
16+
return constructTree(nums, 0, nums.length-1);
1417
}
1518

16-
public TreeNode constructMaximumBinaryTreeImpl(int[] nums, int start, int end) {
17-
19+
private TreeNode constructTree(int[] nums, int start, int end) {
1820
if (start > end) {
1921
return null;
2022
}
2123

2224
int idx = start;
23-
24-
for (int i = start + 1; i <= end; i++) {
25+
for (int i=start+1; i<=end; i++) {
2526
if (nums[i] > nums[idx]) {
2627
idx = i;
2728
}
2829
}
2930

3031
TreeNode root = new TreeNode(nums[idx]);
3132

32-
root.left = constructMaximumBinaryTreeImpl(nums, start, idx-1);
33-
34-
root.right = constructMaximumBinaryTreeImpl(nums, idx+1, end);
33+
root.left = constructTree(nums, start, idx-1);
34+
root.right = constructTree(nums, idx+1, end);
3535

3636
return root;
3737
}

0 commit comments

Comments
 (0)