Skip to content

Commit 7d3b21e

Browse files
author
Shuo
authored
Merge pull request #651 from openset/develop
Add: Add One Row to Tree
2 parents abc9bd9 + b52dc20 commit 7d3b21e

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

internal/kit/tree_node.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func TreeNode2SliceInt(t *TreeNode) (s []int) {
4848
s = append(s, queue[0].Val)
4949
if queue[0].Left != nil || queue[0].Right != nil {
5050
queue = append(queue, queue[0].Left)
51-
}
52-
if queue[0].Right != nil {
5351
queue = append(queue, queue[0].Right)
5452
}
5553
} else {
@@ -58,5 +56,8 @@ func TreeNode2SliceInt(t *TreeNode) (s []int) {
5856
queue = queue[1:]
5957
}
6058
}
59+
for i := len(s) - 1; i >= 0 && s[i] == NULL; i-- {
60+
s = s[:i]
61+
}
6162
return
6263
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
package add_one_row_to_tree
2+
3+
import "github.com/openset/leetcode/internal/kit"
4+
5+
type TreeNode = kit.TreeNode
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
func addOneRow(root *TreeNode, v int, d int) *TreeNode {
16+
switch d {
17+
case 1:
18+
return &TreeNode{Val: v, Left: root}
19+
case 2:
20+
root.Left = &TreeNode{Val: v, Left: root.Left}
21+
root.Right = &TreeNode{Val: v, Right: root.Right}
22+
default:
23+
if root.Left != nil {
24+
root.Left = addOneRow(root.Left, v, d-1)
25+
}
26+
if root.Right != nil {
27+
root.Right = addOneRow(root.Right, v, d-1)
28+
}
29+
}
30+
return root
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
11
package add_one_row_to_tree
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/openset/leetcode/internal/kit"
8+
)
9+
10+
type caseType struct {
11+
input []int
12+
v int
13+
d int
14+
expected []int
15+
}
16+
17+
func TestAddOneRow(t *testing.T) {
18+
tests := [...]caseType{
19+
{
20+
input: []int{4, 2, 6, 3, 1, 5},
21+
v: 1,
22+
d: 2,
23+
expected: []int{4, 1, 1, 2, kit.NULL, kit.NULL, 6, 3, 1, 5},
24+
},
25+
{
26+
input: []int{4, 2, kit.NULL, 3, 1},
27+
v: 1,
28+
d: 3,
29+
expected: []int{4, 2, kit.NULL, 1, 1, 3, kit.NULL, kit.NULL, 1},
30+
},
31+
{
32+
input: []int{3, 1},
33+
v: 1,
34+
d: 1,
35+
expected: []int{1, 3, kit.NULL, 1},
36+
},
37+
{
38+
input: []int{3, kit.NULL, 2},
39+
v: 1,
40+
d: 3,
41+
expected: []int{3, kit.NULL, 2, 1, 1},
42+
},
43+
}
44+
for _, tc := range tests {
45+
root := kit.SliceInt2TreeNode(tc.input)
46+
root = addOneRow(root, tc.v, tc.d)
47+
output := kit.TreeNode2SliceInt(root)
48+
if !reflect.DeepEqual(output, tc.expected) {
49+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
50+
}
51+
}
52+
}

problems/convert-sorted-array-to-binary-search-tree/convert_sorted_array_to_binary_search_tree.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package convert_sorted_array_to_binary_search_tree
22

3-
import . "github.com/openset/leetcode/internal/kit"
3+
import "github.com/openset/leetcode/internal/kit"
4+
5+
type TreeNode = kit.TreeNode
46

57
/**
68
* Definition for a binary tree node.

problems/convert-sorted-array-to-binary-search-tree/convert_sorted_array_to_binary_search_tree_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55
"testing"
66

7-
. "github.com/openset/leetcode/internal/kit"
7+
"github.com/openset/leetcode/internal/kit"
88
)
99

1010
type caseType struct {
@@ -16,11 +16,11 @@ func TestSortedArrayToBST(t *testing.T) {
1616
tests := [...]caseType{
1717
{
1818
input: []int{-10, -3, 0, 5, 9},
19-
expected: []int{0, -3, 9, -10, 5},
19+
expected: []int{0, -3, 9, -10, kit.NULL, 5},
2020
},
2121
}
2222
for _, tc := range tests {
23-
output := TreeNode2SliceInt(sortedArrayToBST(tc.input))
23+
output := kit.TreeNode2SliceInt(sortedArrayToBST(tc.input))
2424
if !reflect.DeepEqual(output, tc.expected) {
2525
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
2626
}

0 commit comments

Comments
 (0)