package main import ( "fmt" ) // TreeNode ... type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func maxInt(x, y int) int { if x > y { return x } return y } func maxDepthIter(curr *TreeNode, depth int) int { if curr == nil { return depth } return maxInt(maxDepthIter(curr.Left, depth+1), maxDepthIter(curr.Right, depth+1)) } /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func maxDepth(root *TreeNode) int { return maxDepthIter(root, 0) } func main() { /** * t1: 5 * / \ * 1 4 * / \ * 2 3 */ t1l := TreeNode{1, nil, nil} t1rl := TreeNode{2, nil, nil} t1rr := TreeNode{3, nil, nil} t1r := TreeNode{4, &t1rl, &t1rr} t1 := &TreeNode{5, &t1l, &t1r} fmt.Println(maxDepth(t1)) }