104.go 902 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. // TreeNode ...
  3. // type TreeNode struct {
  4. // Val int
  5. // Left *TreeNode
  6. // Right *TreeNode
  7. // }
  8. // func maxInt(x, y int) int {
  9. // if x > y {
  10. // return x
  11. // }
  12. // return y
  13. // }
  14. func maxDepthIter(curr *TreeNode, depth int) int {
  15. if curr == nil {
  16. return depth
  17. }
  18. return maxInt(maxDepthIter(curr.Left, depth+1), maxDepthIter(curr.Right, depth+1))
  19. }
  20. /**
  21. * Definition for a binary tree node.
  22. * type TreeNode struct {
  23. * Val int
  24. * Left *TreeNode
  25. * Right *TreeNode
  26. * }
  27. */
  28. func maxDepth(root *TreeNode) int {
  29. return maxDepthIter(root, 0)
  30. }
  31. // func main() {
  32. // /**
  33. // * t1: 5
  34. // * / \
  35. // * 1 4
  36. // * / \
  37. // * 2 3
  38. // */
  39. // t1l := TreeNode{1, nil, nil}
  40. // t1rl := TreeNode{2, nil, nil}
  41. // t1rr := TreeNode{3, nil, nil}
  42. // t1r := TreeNode{4, &t1rl, &t1rr}
  43. // t1 := &TreeNode{5, &t1l, &t1r}
  44. // fmt.Println(maxDepth(t1))
  45. // }