本文主要是介绍Golang | Leetcode Golang题解之第110题平衡二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
func isBalanced(root *TreeNode) bool {return height(root) >= 0
}func height(root *TreeNode) int {if root == nil {return 0}leftHeight := height(root.Left)rightHeight := height(root.Right)if leftHeight == -1 || rightHeight == -1 || abs(leftHeight - rightHeight) > 1 {return -1}return max(leftHeight, rightHeight) + 1
}func max(x, y int) int {if x > y {return x}return y
}func abs(x int) int {if x < 0 {return -1 * x}return x
}
这篇关于Golang | Leetcode Golang题解之第110题平衡二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!