From 770328ecd0673c2f5ba887f3282a75d8aebd3ab5 Mon Sep 17 00:00:00 2001 From: QuinnDK <39618652+QuinnDK@users.noreply.github.com> Date: Thu, 13 May 2021 10:10:53 +0800 Subject: [PATCH] =?UTF-8?q?Update=200110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index 5d55910c..919ec7c7 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -361,7 +361,40 @@ Python: Go: - +```Go +func isBalanced(root *TreeNode) bool { + if root==nil{ + return true + } + if !isBalanced(root.Left) || !isBalanced(root.Right){ + return false + } + LeftH:=maxdepth(root.Left)+1 + RightH:=maxdepth(root.Right)+1 + if abs(LeftH-RightH)>1{ + return false + } + return true +} +func maxdepth(root *TreeNode)int{ + if root==nil{ + return 0 + } + return max(maxdepth(root.Left),maxdepth(root.Right))+1 +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +func abs(a int)int{ + if a<0{ + return -a + } + return a +} +```