add new solutions
This commit is contained in:
parent
f960020cb4
commit
14cd11f1c7
4 changed files with 155 additions and 0 deletions
37
solutions/1/q199/solution.go
Normal file
37
solutions/1/q199/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q199
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func rightSideView(root *TreeNode) []int {
|
||||
ret := []int{}
|
||||
|
||||
type qElem struct {
|
||||
height int
|
||||
node *TreeNode
|
||||
}
|
||||
|
||||
queue := []qElem{{0, root}}
|
||||
var last *qElem
|
||||
for ; len(queue) > 0; queue = queue[1:] {
|
||||
cur := &queue[0]
|
||||
if cur.node == nil {
|
||||
continue
|
||||
}
|
||||
if last != nil && last.height < cur.height {
|
||||
ret = append(ret, last.node.Val)
|
||||
}
|
||||
last = cur
|
||||
queue = append(queue, qElem{cur.height + 1, cur.node.Left}, qElem{cur.height + 1, cur.node.Right})
|
||||
}
|
||||
if last != nil {
|
||||
ret = append(ret, last.node.Val)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = rightSideView
|
||||
Loading…
Add table
Add a link
Reference in a new issue