Skip to content

Commit a4933d8

Browse files
committed
leetcode
1 parent d37ed46 commit a4933d8

File tree

4 files changed

+568
-0
lines changed

4 files changed

+568
-0
lines changed
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/*
2+
3+
-* Add One Row to Tree *-
4+
5+
Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.
6+
7+
Note that the root node is at depth 1.
8+
9+
The adding rule is:
10+
11+
Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
12+
cur's original left subtree should be the left subtree of the new left subtree root.
13+
cur's original right subtree should be the right subtree of the new right subtree root.
14+
If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
15+
16+
17+
Example 1:
18+
19+
20+
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
21+
Output: [4,1,1,2,null,null,6,3,1,5]
22+
Example 2:
23+
24+
25+
Input: root = [4,2,null,3,1], val = 1, depth = 3
26+
Output: [4,2,null,1,1,3,null,null,1]
27+
28+
29+
Constraints:
30+
31+
The number of nodes in the tree is in the range [1, 104].
32+
The depth of the tree is in the range [1, 104].
33+
-100 <= Node.val <= 100
34+
-105 <= val <= 105
35+
1 <= depth <= the depth of tree + 1
36+
37+
*/
38+
39+
// Definition for a binary tree node.
40+
import 'dart:collection';
41+
42+
class TreeNode {
43+
int val;
44+
TreeNode? left;
45+
TreeNode? right;
46+
TreeNode([this.val = 0, this.left, this.right]);
47+
}
48+
49+
class A {
50+
// Recursive
51+
// Runtime: 586 ms, faster than 100.00% of Dart online submissions for Add One Row to Tree.
52+
// Memory Usage: 146.9 MB, less than 100.00% of Dart online submissions for Add One Row to Tree.
53+
void walk(TreeNode? root, int val, int depth, int c) {
54+
// if our root is not null means we have some value to begin with
55+
if (root != null) {
56+
if (c == depth - 1) {
57+
/// creating the temp node to hold the value of our left node
58+
TreeNode? temp = root.left;
59+
// left side of the tree will hold the value of the all the tree node
60+
root.left = TreeNode(val);
61+
// now will we will assign he value of the far left side of the tree to our temp node
62+
root.left?.left = temp;
63+
// swapping the nodes value left to right
64+
temp = root.right;
65+
// right side of the node will hold the tree node value
66+
root.right = TreeNode(val);
67+
// than we will assign the value of far right side of the tree to the temp node we created
68+
root.right?.right = temp;
69+
return;
70+
}
71+
// than we will walk the tree left to right with it's depth
72+
walk(root.left, val, depth, c + 1);
73+
walk(root.right, val, depth, c + 1);
74+
}
75+
}
76+
77+
TreeNode? addOneRow(TreeNode? root, int val, int depth) {
78+
if (depth == 1) {
79+
// temp node to hold the value to tree node
80+
TreeNode? tempNode = TreeNode(val);
81+
tempNode.left = root;
82+
return tempNode;
83+
}
84+
// walking from the root to each value and depth
85+
walk(root, val, depth, 1);
86+
return root;
87+
}
88+
}
89+
90+
class B {
91+
// In Order Traversal
92+
// Runtime: 736 ms, faster than 100.00% of Dart online submissions for Add One Row to Tree.
93+
// Memory Usage: 147.4 MB, less than 100.00% of Dart online submissions for Add One Row to Tree.
94+
TreeNode? addOneRow(TreeNode? root, int val, int depth) {
95+
TreeNode? inOrder(TreeNode? node, [height = 1, child = '']) {
96+
if (node == null) {
97+
return height == depth ? TreeNode(val) : node;
98+
}
99+
100+
if (height == depth) {
101+
// When height matches, insert new node.
102+
// Also parent has passed which child it is, left or right.
103+
// Append current tree at appropriate position
104+
TreeNode newNode = TreeNode(val);
105+
if (child == 'L' || child == '') {
106+
newNode.left = node;
107+
} else if (child == 'R') {
108+
newNode.right = node;
109+
}
110+
return newNode;
111+
} else if (height < depth) {
112+
node.left = inOrder(node.left, height + 1, 'L');
113+
node.right = inOrder(node.right, height + 1, 'R');
114+
return node;
115+
}
116+
117+
return node;
118+
}
119+
120+
return inOrder(root);
121+
}
122+
}
123+
124+
class C {
125+
// Depth First search - DFS
126+
// Runtime: 675 ms, faster than 100.00% of Dart online submissions for Add One Row to Tree.
127+
// Memory Usage: 149.4 MB, less than 100.00% of Dart online submissions for Add One Row to Tree.
128+
void walk(TreeNode? currentNode, TreeNode? parentNode, bool left, int val,
129+
int maxDepth, int currentDepth) {
130+
// Base case 1
131+
if (currentDepth == maxDepth - 1) {
132+
TreeNode node = createNode(val);
133+
// Base case 2
134+
if (left) {
135+
node.left = parentNode?.left;
136+
parentNode?.left = node;
137+
} else {
138+
// Base case 3
139+
node.right = parentNode?.right;
140+
parentNode?.right = node;
141+
}
142+
return;
143+
}
144+
//Base case
145+
if (currentNode == null) {
146+
return;
147+
}
148+
// Left sub tree
149+
walk(currentNode.left, currentNode, true, val, maxDepth, currentDepth + 1);
150+
// right sub tree
151+
walk(
152+
currentNode.right, currentNode, false, val, maxDepth, currentDepth + 1);
153+
}
154+
155+
TreeNode createNode(int val) {
156+
return TreeNode(val);
157+
}
158+
159+
TreeNode? addOneRow(TreeNode? root, int val, int depth) {
160+
// Base case 4
161+
if (depth == 1) {
162+
TreeNode node = createNode(val);
163+
node.left = root;
164+
return node;
165+
}
166+
// go for left sub tree.
167+
walk(root?.left, root, true, val, depth, 1);
168+
// go for right sub tree.
169+
walk(root?.right, root, false, val, depth, 1);
170+
return root;
171+
}
172+
}
173+
174+
class D {
175+
// Runtime: 523 ms, faster than 100.00% of Dart online submissions for Add One Row to Tree.
176+
// Memory Usage: 143.8 MB, less than 100.00% of Dart online submissions for Add One Row to Tree.
177+
void levelOrder(TreeNode? root, int val, int depth, int lvl) {
178+
if (root == null) return;
179+
if (lvl == depth - 2) {
180+
TreeNode? l = root.left, r = root.right;
181+
root.left = TreeNode(val);
182+
root.left?.left = l;
183+
root.right = TreeNode(val);
184+
root.right?.right = r;
185+
}
186+
levelOrder(root.left, val, depth, lvl + 1);
187+
levelOrder(root.right, val, depth, lvl + 1);
188+
}
189+
190+
TreeNode? addOneRow(TreeNode? root, int val, int depth) {
191+
if (depth == 1) {
192+
TreeNode? node = new TreeNode(val);
193+
node.left = root;
194+
return node;
195+
}
196+
levelOrder(root, val, depth, 0);
197+
return root;
198+
}
199+
}
200+
201+
class E {
202+
// Runtime: 547 ms, faster than 100.00% of Dart online submissions for Add One Row to Tree.
203+
// Memory Usage: 146.9 MB, less than 100.00% of Dart online submissions for Add One Row to Tree.
204+
TreeNode? addOneRow(TreeNode? root, int val, int depth) {
205+
// if the depth of the tree is only one
206+
if (depth == 1) {
207+
// node will hold our value
208+
TreeNode node = TreeNode(val);
209+
// on thee left side
210+
node.left = root;
211+
// and we will add the node
212+
return node;
213+
}
214+
215+
Queue<TreeNode?> queue = Queue();
216+
// add the tree root inn uu queue
217+
queue.add(root);
218+
// FOR EXAMPLE IF THE depth is only ne
219+
int currentDepth = 1;
220+
// assuming that the queue is not empty
221+
while (queue.isNotEmpty) {
222+
// getting the whole length of the queue
223+
int size = queue.length;
224+
currentDepth++;
225+
226+
if (currentDepth == depth) {
227+
for (int i = 0; i < size; i++) {
228+
// removing the first value from the queue
229+
TreeNode? node = queue.removeFirst();
230+
// our new left node
231+
TreeNode? newLeftNode = TreeNode(val);
232+
// assigning the new left node to the left side of the tree node
233+
newLeftNode.left = node?.left;
234+
node?.left = newLeftNode;
235+
// new right node
236+
TreeNode? newRightNode = TreeNode(val);
237+
// assigning the new right node to the left side of the tree node
238+
newRightNode.right = node?.right;
239+
node?.right = newRightNode;
240+
}
241+
242+
break;
243+
} else {
244+
// iterating through the whole length of the queue
245+
for (int i = 0; i < size; i++) {
246+
// removing the first value
247+
TreeNode? node = queue.removeFirst();
248+
// if left side is not null we will add the value to left side
249+
if (node?.left != null) queue.add(node?.left);
250+
// if left side is not null we will add the value to left side
251+
if (node?.right != null) queue.add(node?.right);
252+
}
253+
}
254+
}
255+
256+
return root;
257+
}
258+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
// Definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
11+
if depth == 1 {
12+
return &TreeNode{
13+
Val: val,
14+
Left: root,
15+
Right: nil,
16+
}
17+
}
18+
19+
var queue []*TreeNode
20+
queue = append(queue, root)
21+
22+
currentDepth := 1
23+
24+
for len(queue) > 0 {
25+
currentDepth++
26+
length := len(queue)
27+
28+
for i := 0; i < length; i++ {
29+
if currentDepth == depth {
30+
left := queue[i].Left
31+
right := queue[i].Right
32+
33+
newLeftNode := &TreeNode{
34+
Val: val,
35+
Left: nil,
36+
Right: nil,
37+
}
38+
39+
newRightNode := &TreeNode{
40+
Val: val,
41+
Left: nil,
42+
Right: nil,
43+
}
44+
45+
queue[i].Left = newLeftNode
46+
queue[i].Right = newRightNode
47+
48+
newLeftNode.Left = left
49+
newRightNode.Right = right
50+
}
51+
52+
if queue[i].Left != nil {
53+
queue = append(queue, queue[i].Left)
54+
}
55+
56+
if queue[i].Right != nil {
57+
queue = append(queue, queue[i].Right)
58+
}
59+
}
60+
61+
queue = queue[length:]
62+
}
63+
64+
return root
65+
}

0 commit comments

Comments
 (0)