File tree Expand file tree Collapse file tree 1 file changed +0
-66
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +0
-66
lines changed Original file line number Diff line number Diff line change 22
33import com .fishercoder .common .classes .TreeNode ;
44
5- /**
6- * 623. Add One Row to Tree
7- *
8- * Given the root of a binary tree,
9- * then value v and depth d,
10- * you need to add a row of nodes with value v at the given depth d.
11- * The root node is at depth 1.
12-
13- The adding rule is: given a positive integer depth d,
14- for each NOT null tree nodes N in depth d-1,
15- create two tree nodes with value v as N's left subtree root and right subtree root.
16- And N's original left subtree should be the left subtree of the new left subtree root,
17- its original right subtree should be the right subtree of the new right subtree root.
18- If depth d is 1 that means there is no depth d-1 at all,
19- then create a tree node with value v as the new root of the whole
20- original tree, and the original tree is the new root's left subtree.
21-
22- Example 1:
23- Input:
24- A binary tree as following:
25- 4
26- / \
27- 2 6
28- / \ /
29- 3 1 5
30-
31- v = 1
32-
33- d = 2
34-
35- Output:
36- 4
37- / \
38- 1 1
39- / \
40- 2 6
41- / \ /
42- 3 1 5
43-
44-
45- Example 2:
46- Input:
47- A binary tree as following:
48- 4
49- /
50- 2
51- / \
52- 3 1
53-
54- v = 1
55-
56- d = 3
57-
58- Output:
59- 4
60- /
61- 2
62- / \
63- 1 1
64- / \
65- 3 1
66-
67- Note:
68- The given d is in range [1, maximum depth of the given tree + 1].
69- The given binary tree has at least one tree node.
70- */
715public class _623 {
726
737 public static class Solution1 {
You can’t perform that action at this time.
0 commit comments