Skip to content

Commit 699bd2e

Browse files
committed
Added 5 solutions
1 parent 680588b commit 699bd2e

5 files changed

+254
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
int ans = 0;
12+
double minDiff = Double.MAX_VALUE;
13+
14+
public int closestValue(TreeNode root, double target) {
15+
updateVal(root, target);
16+
return ans;
17+
}
18+
19+
private void updateVal(TreeNode root, double target) {
20+
if (root == null) {
21+
return;
22+
}
23+
24+
if (Math.abs(root.val - target) < minDiff) {
25+
minDiff = Math.abs(root.val - target);
26+
ans = root.val;
27+
}
28+
29+
if (root.val < target) {
30+
updateVal(root.right, target);
31+
}
32+
else {
33+
updateVal(root.left, target);
34+
}
35+
}
36+
}

Easy/Nested List Weight Sum.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* public interface NestedInteger {
5+
* // Constructor initializes an empty nested list.
6+
* public NestedInteger();
7+
*
8+
* // Constructor initializes a single integer.
9+
* public NestedInteger(int value);
10+
*
11+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
12+
* public boolean isInteger();
13+
*
14+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
15+
* // Return null if this NestedInteger holds a nested list
16+
* public Integer getInteger();
17+
*
18+
* // Set this NestedInteger to hold a single integer.
19+
* public void setInteger(int value);
20+
*
21+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22+
* public void add(NestedInteger ni);
23+
*
24+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
25+
* // Return null if this NestedInteger holds a single integer
26+
* public List<NestedInteger> getList();
27+
* }
28+
*/
29+
class Solution {
30+
int sum = 0;
31+
32+
public int depthSum(List<NestedInteger> nestedList) {
33+
for (NestedInteger n : nestedList) {
34+
dfsHelper(n, 1);
35+
}
36+
37+
return sum;
38+
}
39+
40+
private void dfsHelper(NestedInteger n, int depth) {
41+
if (n.isInteger()) {
42+
sum += n.getInteger() * depth;
43+
}
44+
else {
45+
for (NestedInteger i : n.getList()) {
46+
dfsHelper(i, depth+1);
47+
}
48+
}
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
public List<List<Integer>> findLeaves(TreeNode root) {
12+
List<List<Integer>> list = new ArrayList<>();
13+
14+
while (root != null) {
15+
List<Integer> leaves = getLeaves(root);
16+
root = removeLeaves(root);
17+
list.add(leaves);
18+
}
19+
20+
return list;
21+
}
22+
23+
private TreeNode removeLeaves(TreeNode root) {
24+
if (root == null) {
25+
return null;
26+
}
27+
28+
if (root.left == null && root.right == null) {
29+
return null;
30+
}
31+
32+
root.left = removeLeaves(root.left);
33+
root.right = removeLeaves(root.right);
34+
35+
return root;
36+
}
37+
38+
private List<Integer> getLeaves(TreeNode root) {
39+
Queue<TreeNode> queue = new LinkedList<>();
40+
queue.add(root);
41+
List<Integer> list = new ArrayList<>();
42+
43+
while (!queue.isEmpty()) {
44+
TreeNode node = queue.remove();
45+
if (node.left == null && node.right == null) {
46+
list.add(node.val);
47+
}
48+
else {
49+
if (node.left != null) {
50+
queue.add(node.left);
51+
}
52+
53+
if (node.right != null) {
54+
queue.add(node.right);
55+
}
56+
}
57+
}
58+
59+
return list;
60+
}
61+
}

Medium/Inorder Successor in BST.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
12+
if (p.right != null) {
13+
TreeNode node = p.right;
14+
while (node.left != null) {
15+
node = node.left;
16+
}
17+
18+
return node;
19+
}
20+
21+
return getLastLeft(root, p);
22+
}
23+
24+
private TreeNode getLastLeft(TreeNode root, TreeNode p) {
25+
TreeNode lastLeft = null;
26+
27+
while (root != null) {
28+
if (root == p) {
29+
break;
30+
}
31+
else if (root.val < p.val) {
32+
root = root.right;
33+
}
34+
else {
35+
lastLeft = root;
36+
root = root.left;
37+
}
38+
}
39+
40+
return lastLeft;
41+
}
42+
}

Medium/Nested List Weight Sum II.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* public interface NestedInteger {
5+
* // Constructor initializes an empty nested list.
6+
* public NestedInteger();
7+
*
8+
* // Constructor initializes a single integer.
9+
* public NestedInteger(int value);
10+
*
11+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
12+
* public boolean isInteger();
13+
*
14+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
15+
* // Return null if this NestedInteger holds a nested list
16+
* public Integer getInteger();
17+
*
18+
* // Set this NestedInteger to hold a single integer.
19+
* public void setInteger(int value);
20+
*
21+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
22+
* public void add(NestedInteger ni);
23+
*
24+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
25+
* // Return null if this NestedInteger holds a single integer
26+
* public List<NestedInteger> getList();
27+
* }
28+
*/
29+
class Solution {
30+
int maxDepth = 1;
31+
int sum = 0;
32+
public int depthSumInverse(List<NestedInteger> nestedList) {
33+
for (NestedInteger n : nestedList) {
34+
updateMaxDepth(n, 1);
35+
}
36+
37+
for (NestedInteger n : nestedList) {
38+
dfsHelper(n, maxDepth);
39+
}
40+
41+
return sum;
42+
}
43+
44+
private void dfsHelper(NestedInteger n, int depth) {
45+
if (n.isInteger()) {
46+
sum += n.getInteger() * depth;
47+
}
48+
else {
49+
for (NestedInteger i : n.getList()) {
50+
dfsHelper(i, depth-1);
51+
}
52+
}
53+
}
54+
55+
private void updateMaxDepth(NestedInteger n, int depth) {
56+
if (n.isInteger()) {
57+
maxDepth = Math.max(maxDepth, depth);
58+
}
59+
else {
60+
for (NestedInteger i : n.getList()) {
61+
updateMaxDepth(i, depth+1);
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)