add new solutions
This commit is contained in:
parent
0c73608ce5
commit
d798d5e8c9
19 changed files with 661 additions and 4 deletions
55
solutions/1/q103/solution.go
Normal file
55
solutions/1/q103/solution.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package q103
|
||||
|
||||
import "slices"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func zigzagLevelOrder(root *TreeNode) [][]int {
|
||||
ret := [][]int{}
|
||||
if root == nil {
|
||||
return ret
|
||||
}
|
||||
|
||||
dpth := 1
|
||||
buf := make([]int, 512)
|
||||
row := buf[:0]
|
||||
|
||||
type qElem struct {
|
||||
depth int
|
||||
node *TreeNode
|
||||
}
|
||||
queue := make([]qElem, 0, 256)
|
||||
queue = append(queue, qElem{depth: 1, node: root})
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
c := queue[0]
|
||||
if c.depth > dpth {
|
||||
if dpth%2 == 0 {
|
||||
slices.Reverse(row)
|
||||
}
|
||||
ret = append(ret, make([]int, len(row)))
|
||||
copy(ret[len(ret)-1], row)
|
||||
|
||||
dpth = c.depth
|
||||
row = buf[:0]
|
||||
}
|
||||
|
||||
row = append(row, c.node.Val)
|
||||
if c.node.Left != nil {
|
||||
queue = append(queue, qElem{depth: c.depth + 1, node: c.node.Left})
|
||||
}
|
||||
if c.node.Right != nil {
|
||||
queue = append(queue, qElem{depth: c.depth + 1, node: c.node.Right})
|
||||
}
|
||||
}
|
||||
if dpth%2 == 0 {
|
||||
slices.Reverse(row)
|
||||
}
|
||||
ret = append(ret, row)
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = zigzagLevelOrder
|
||||
26
solutions/1/q106/solution.go
Normal file
26
solutions/1/q106/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q106
|
||||
|
||||
import "slices"
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func buildTree(inorder []int, postorder []int) *TreeNode {
|
||||
if len(inorder) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
val := postorder[len(postorder)-1]
|
||||
pos := slices.Index(inorder, val)
|
||||
|
||||
return &TreeNode{
|
||||
Val: val,
|
||||
Left: buildTree(inorder[0:pos], postorder[:pos]),
|
||||
Right: buildTree(inorder[pos+1:], postorder[pos:len(postorder)-1]),
|
||||
}
|
||||
}
|
||||
|
||||
var _ = buildTree
|
||||
34
solutions/1/q117/solution.go
Normal file
34
solutions/1/q117/solution.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package q117
|
||||
|
||||
type Node struct {
|
||||
Val int
|
||||
Left *Node
|
||||
Right *Node
|
||||
Next *Node
|
||||
}
|
||||
|
||||
func walk(node *Node, depth int, depths *[]*Node) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if depth > len(*depths) {
|
||||
*depths = append(*depths, nil)
|
||||
}
|
||||
if (*depths)[depth-1] != nil {
|
||||
(*depths)[depth-1].Next = node
|
||||
}
|
||||
(*depths)[depth-1] = node
|
||||
|
||||
walk(node.Left, depth+1, depths)
|
||||
walk(node.Right, depth+1, depths)
|
||||
}
|
||||
|
||||
func connect(root *Node) *Node {
|
||||
depths := []*Node{}
|
||||
walk(root, 1, &depths)
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
var _ = connect
|
||||
26
solutions/1/q118/solution.go
Normal file
26
solutions/1/q118/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q118
|
||||
|
||||
func generate(numRows int) [][]int {
|
||||
buf := make([]int, numRows*(1+numRows)/2)
|
||||
p := 0
|
||||
take := func(n int) []int {
|
||||
ret := buf[p : p+n]
|
||||
p = p + n
|
||||
return ret
|
||||
}
|
||||
|
||||
ret := make([][]int, 0, numRows)
|
||||
|
||||
for i := 1; i <= numRows; i++ {
|
||||
row := take(i)
|
||||
row[0] = 1
|
||||
row[len(row)-1] = 1
|
||||
for j := 1; j < len(row)-1; j++ {
|
||||
row[j] = ret[i-2][j-1] + ret[i-2][j]
|
||||
}
|
||||
ret = append(ret, row)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = generate
|
||||
33
solutions/1/q129/solution.go
Normal file
33
solutions/1/q129/solution.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package q129
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func sumNums(node *TreeNode, prefix int) int {
|
||||
prefix = prefix*10 + node.Val
|
||||
|
||||
if node.Left == nil && node.Right == nil {
|
||||
return prefix
|
||||
}
|
||||
|
||||
sum := 0
|
||||
if node.Left != nil {
|
||||
sum += sumNums(node.Left, prefix)
|
||||
}
|
||||
if node.Right != nil {
|
||||
sum += sumNums(node.Right, prefix)
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func sumNumbers(root *TreeNode) int {
|
||||
if root == nil {
|
||||
return 0
|
||||
}
|
||||
return sumNums(root, 0)
|
||||
}
|
||||
|
||||
var _ = sumNumbers
|
||||
32
solutions/1/q148/solution.go
Normal file
32
solutions/1/q148/solution.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package q148
|
||||
|
||||
import "slices"
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func sortList(head *ListNode) *ListNode {
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
arr := []*ListNode{}
|
||||
for head != nil {
|
||||
arr = append(arr, head)
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
slices.SortFunc(arr, func(a, b *ListNode) int { return a.Val - b.Val })
|
||||
for i := range arr {
|
||||
if i == len(arr)-1 {
|
||||
arr[i].Next = nil
|
||||
} else {
|
||||
arr[i].Next = arr[i+1]
|
||||
}
|
||||
}
|
||||
return arr[0]
|
||||
}
|
||||
|
||||
var _ = sortList
|
||||
65
solutions/1/q173/solution.go
Normal file
65
solutions/1/q173/solution.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package q173
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
type stackElem struct {
|
||||
state uint8 // 0, 1, 2, 3
|
||||
node *TreeNode
|
||||
}
|
||||
|
||||
type BSTIterator struct {
|
||||
stack []stackElem
|
||||
next *TreeNode
|
||||
}
|
||||
|
||||
func Constructor(root *TreeNode) BSTIterator {
|
||||
return BSTIterator{stack: []stackElem{{node: root}}}
|
||||
}
|
||||
|
||||
func (it *BSTIterator) Next() int {
|
||||
it.findNext()
|
||||
ret := it.next.Val
|
||||
it.next = nil
|
||||
return ret
|
||||
}
|
||||
|
||||
func (it *BSTIterator) HasNext() bool {
|
||||
it.findNext()
|
||||
return it.next != nil
|
||||
}
|
||||
|
||||
func (it *BSTIterator) findNext() {
|
||||
for it.next == nil && len(it.stack) > 0 {
|
||||
c := &it.stack[len(it.stack)-1]
|
||||
c.state++
|
||||
|
||||
switch c.state - 1 {
|
||||
case 0: // init
|
||||
if c.node.Left != nil {
|
||||
it.stack = append(it.stack, stackElem{node: c.node.Left})
|
||||
}
|
||||
|
||||
case 1: // left processed
|
||||
it.next = c.node
|
||||
|
||||
case 2: // middle processed
|
||||
if c.node.Right != nil {
|
||||
it.stack = append(it.stack, stackElem{node: c.node.Right})
|
||||
}
|
||||
|
||||
case 3: // right processed
|
||||
it.stack = it.stack[:len(it.stack)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your BSTIterator object will be instantiated and called as such:
|
||||
* obj := Constructor(root);
|
||||
* param_1 := obj.Next();
|
||||
* param_2 := obj.HasNext();
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue