123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package main
- import (
- "strconv"
- )
- // ListNode ...
- type ListNode struct {
- Val int
- Next *ListNode
- }
- // TreeNode ...
- type TreeNode struct {
- Val int
- Left *TreeNode
- Right *TreeNode
- }
- func abs(x int) int {
- if x < 0 {
- return -x
- }
- return x
- }
- func maxInt(x, y int) int {
- if x > y {
- return x
- }
- return y
- }
- func minInt(x, y int) int {
- if x < y {
- return x
- }
- return y
- }
- func toLinkedList(num []int) *ListNode {
- length := len(num)
- if length == 0 {
- return nil
- }
- head := ListNode{num[0], nil}
- curr := &head
- for i := 1; i < length; i++ {
- curr.Next = &ListNode{num[i], nil}
- curr = curr.Next
- }
- return &head
- }
- func printList(head *ListNode) {
- curr := head
- for curr != nil {
- print(strconv.FormatInt(int64(curr.Val), 10), " ")
- curr = curr.Next
- }
- println()
- }
- func printTree(root *TreeNode) { // Level order traversal
- if root == nil {
- println("nil")
- return
- }
- queue := make([]*TreeNode, 0) // Important!
- queue = append(queue, root)
- for len(queue) != 0 {
- curr := queue[0]
- queue = queue[1:] // Dequeue
- if curr == nil {
- print("nil ")
- continue
- }
- print(curr.Val, " ")
- if curr.Left != nil {
- queue = append(queue, curr.Left)
- } else if curr.Right != nil {
- queue = append(queue, nil)
- }
- if curr.Right != nil {
- queue = append(queue, curr.Right)
- } else if curr.Left != nil {
- queue = append(queue, nil)
- }
- }
- println()
- }
- func printBoard(board [][]byte) {
- for _, row := range board {
- for _, grid := range row {
- print(string(grid), " ")
- }
- println()
- }
- }
|