1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
LeetCode/java/add-one-row-to-tree.java

36 lines
780 B
Java
Raw Normal View History

class Solution {
private enum Side {
Left,
Right
}
private TreeNode addOneRow(int depth, int val, TreeNode node, int currentDepth, Side s) {
if (depth == currentDepth) {
var newRoot = new TreeNode(val);
switch (s) {
case Left:
newRoot.left = node;
break;
case Right:
newRoot.right = node;
break;
}
return newRoot;
}
if (node == null) {
return node;
}
node.left = addOneRow(depth, val, node.left, currentDepth + 1, Side.Left);
node.right = addOneRow(depth, val, node.right, currentDepth + 1, Side.Right);
return node;
}
public TreeNode addOneRow(TreeNode root, int val, int depth) {
return addOneRow(depth, val, root, 1, Side.Left);
}
}