forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_623.java
38 lines (32 loc) · 1022 Bytes
/
_623.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _623 {
public static class Solution1 {
public TreeNode addOneRow(TreeNode root, int v, int d) {
if (d == 1) {
TreeNode newRoot = new TreeNode(v);
newRoot.left = root;
return newRoot;
} else {
dfs(root, v, d);
return root;
}
}
private void dfs(TreeNode root, int v, int d) {
if (root == null) {
return;
}
if (d == 2) {
TreeNode newLeft = new TreeNode(v);
TreeNode newRight = new TreeNode(v);
newLeft.left = root.left;
newRight.right = root.right;
root.left = newLeft;
root.right = newRight;
} else {
dfs(root.left, v, d - 1);
dfs(root.right, v, d - 1);
}
}
}
}