Golang Solution


  • 0
    J
    type TreeNode struct {
    	Val   int
    	Left  *TreeNode
    	Right *TreeNode
    }
    
    func max(a int, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    func diameterOfBinaryTree(root *TreeNode) int {
    	if root == nil {
    		return 0
    	}
    	return max(max(diameterOfBinaryTree(root.Left), diameterOfBinaryTree(root.Right)), height(root.Left) + height(root.Right))
    }
    
    func height(node *TreeNode) int {
    	if node == nil {
    		return 0
    	}
    	return 1 + max(height(node.Left), height(node.Right))
    }
    

Log in to reply
 

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