mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 110.平衡二叉树,添加C#版
This commit is contained in:
@ -908,6 +908,31 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
public bool IsBalanced(TreeNode root)
|
||||||
|
{
|
||||||
|
return GetHeight(root) == -1 ? false : true;
|
||||||
|
}
|
||||||
|
public int GetHeight(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int left = GetHeight(root.left);
|
||||||
|
if (left == -1) return -1;
|
||||||
|
int right = GetHeight(root.right);
|
||||||
|
if (right == -1) return -1;
|
||||||
|
int res;
|
||||||
|
if (Math.Abs(left - right) > 1)
|
||||||
|
{
|
||||||
|
res = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = 1 + Math.Max(left, right);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user