-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution623.cs
36 lines (33 loc) · 951 Bytes
/
Solution623.cs
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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution623
{
/// <summary>
/// 623. Add One Row to Tree - Medium
/// <a href="https://leetcode.com/problems/add-one-row-to-tree">See the problem</a>
/// </summary>
public TreeNode AddOneRow(TreeNode root, int val, int depth)
{
if (depth == 1)
{
return new TreeNode(val, root);
}
AddRow(root, val, depth, 1);
return root;
}
private void AddRow(TreeNode node, int val, int depth, int currentDepth)
{
if (node == null)
{
return;
}
if (currentDepth == depth - 1)
{
node.left = new TreeNode(val, node.left);
node.right = new TreeNode(val, null, node.right);
return;
}
AddRow(node.left, val, depth, currentDepth + 1);
AddRow(node.right, val, depth, currentDepth + 1);
}
}