helper.go 726 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package helper
  2. import (
  3. "fmt"
  4. )
  5. type ListNode struct {
  6. Val int
  7. Next *ListNode
  8. }
  9. type TreeNode struct {
  10. Val int
  11. Left *TreeNode
  12. Right *TreeNode
  13. }
  14. func abs(x int) int {
  15. if x < 0 {
  16. return -x
  17. }
  18. return x
  19. }
  20. func maxInt(x, y int) int {
  21. if x > y {
  22. return x
  23. }
  24. return y
  25. }
  26. func minInt(x, y int) int {
  27. if x < y {
  28. return x
  29. }
  30. return y
  31. }
  32. func list2str(head *ListNode) string {
  33. curr := head
  34. str := make([]rune, 0)
  35. for curr != nil {
  36. str = append(str, rune(curr.Val+'0'))
  37. curr = curr.Next
  38. }
  39. return string(str)
  40. }
  41. func printTree(root *TreeNode) {
  42. if root == nil {
  43. return
  44. }
  45. if root.Left != nil {
  46. printTree(root.Left)
  47. }
  48. if root.Right != nil {
  49. printTree(root.Right)
  50. }
  51. fmt.Print(root.Val, " ")
  52. }