helper.go 758 B

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