A golang solution


  • 0
    N
    func findMaxDepth(root *TreeNode, currentMaxDepth *int, currentDepth int)  {
    	currentDepth++
    	if root.Left == nil && root.Right == nil {
    		if currentDepth > *currentMaxDepth {
    			*currentMaxDepth = currentDepth
    		}
    		return 
    	}
    	
    	for _, v := range []*TreeNode{root.Left, root.Right} {
    	    if v != nil {
    		    findMaxDepth(v, currentMaxDepth, currentDepth)
    	    }
    	}
    }
    
    func maxDepth(root *TreeNode) int {
    	if root == nil {
    		return 0
    	}
    	
    	var currentMaxDepth int
    	findMaxDepth(root, &currentMaxDepth, 0)
    	return currentMaxDepth
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.