go解题答案

  • 思路概括:保证 左右树平衡&& 左右差不大于2
  • 思路核心:
    func IsBalanced_Solution( pRoot *TreeNode ) bool {
      return pRoot==nil || IsBalanced_Solution(pRoot.Left)&&
    IsBalanced_Solution(pRoot.Right)  && math.Abs(height(pRoot.Left)-height(pRoot.Right))<2 
    }
    func height(root *TreeNode) float64{
      if root== nil {
          return 0
      }
      return math.Max(height(root.Left),height(root.Right))+1
    }

    如果有帮助请点个赞哦, 更多文章请看我的博客

    题主背景

  • 从业8年——超级内卷500Q技术经理——目前专注go和微服务架构