Skip to content

Add: Add One Row to Tree #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/kit/tree_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ func TreeNode2SliceInt(t *TreeNode) (s []int) {
s = append(s, queue[0].Val)
if queue[0].Left != nil || queue[0].Right != nil {
queue = append(queue, queue[0].Left)
}
if queue[0].Right != nil {
queue = append(queue, queue[0].Right)
}
} else {
Expand All @@ -58,5 +56,8 @@ func TreeNode2SliceInt(t *TreeNode) (s []int) {
queue = queue[1:]
}
}
for i := len(s) - 1; i >= 0 && s[i] == NULL; i-- {
s = s[:i]
}
return
}
30 changes: 30 additions & 0 deletions problems/add-one-row-to-tree/add_one_row_to_tree.go
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
package add_one_row_to_tree

import "github.com/openset/leetcode/internal/kit"

type TreeNode = kit.TreeNode

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func addOneRow(root *TreeNode, v int, d int) *TreeNode {
switch d {
case 1:
return &TreeNode{Val: v, Left: root}
case 2:
root.Left = &TreeNode{Val: v, Left: root.Left}
root.Right = &TreeNode{Val: v, Right: root.Right}
default:
if root.Left != nil {
root.Left = addOneRow(root.Left, v, d-1)
}
if root.Right != nil {
root.Right = addOneRow(root.Right, v, d-1)
}
}
return root
}
51 changes: 51 additions & 0 deletions problems/add-one-row-to-tree/add_one_row_to_tree_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
package add_one_row_to_tree

import (
"reflect"
"testing"

"github.com/openset/leetcode/internal/kit"
)

type caseType struct {
input []int
v int
d int
expected []int
}

func TestAddOneRow(t *testing.T) {
tests := [...]caseType{
{
input: []int{4, 2, 6, 3, 1, 5},
v: 1,
d: 2,
expected: []int{4, 1, 1, 2, kit.NULL, kit.NULL, 6, 3, 1, 5},
},
{
input: []int{4, 2, kit.NULL, 3, 1},
v: 1,
d: 3,
expected: []int{4, 2, kit.NULL, 1, 1, 3, kit.NULL, kit.NULL, 1},
},
{
input: []int{3, 1},
v: 1,
d: 1,
expected: []int{1, 3, kit.NULL, 1},
},
{
input: []int{3, kit.NULL, 2},
v: 1,
d: 3,
expected: []int{3, kit.NULL, 2, 1, 1},
},
}
for _, tc := range tests {
root := kit.SliceInt2TreeNode(tc.input)
root = addOneRow(root, tc.v, tc.d)
output := kit.TreeNode2SliceInt(root)
if !reflect.DeepEqual(output, tc.expected) {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package convert_sorted_array_to_binary_search_tree

import . "github.com/openset/leetcode/internal/kit"
import "github.com/openset/leetcode/internal/kit"

type TreeNode = kit.TreeNode

/**
* Definition for a binary tree node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

. "github.com/openset/leetcode/internal/kit"
"github.com/openset/leetcode/internal/kit"
)

type caseType struct {
Expand All @@ -16,11 +16,11 @@ func TestSortedArrayToBST(t *testing.T) {
tests := [...]caseType{
{
input: []int{-10, -3, 0, 5, 9},
expected: []int{0, -3, 9, -10, 5},
expected: []int{0, -3, 9, -10, kit.NULL, 5},
},
}
for _, tc := range tests {
output := TreeNode2SliceInt(sortedArrayToBST(tc.input))
output := kit.TreeNode2SliceInt(sortedArrayToBST(tc.input))
if !reflect.DeepEqual(output, tc.expected) {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
Expand Down