mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -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">
|
||||||
|
@ -708,6 +708,49 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public int MinDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int left = MinDepth(root.left);
|
||||||
|
int right = MinDepth(root.right);
|
||||||
|
if (root.left == null && root.right != null)
|
||||||
|
return 1+right;
|
||||||
|
else if(root.left!=null && root.right == null)
|
||||||
|
return 1+left;
|
||||||
|
|
||||||
|
int res = 1 + Math.Min(left, right);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 迭代
|
||||||
|
public int MinDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int depth = 0;
|
||||||
|
var que = new Queue<TreeNode>();
|
||||||
|
que.Enqueue(root);
|
||||||
|
while (que.Count > 0)
|
||||||
|
{
|
||||||
|
int size = que.Count;
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var node = que.Dequeue();
|
||||||
|
if (node.left != null)
|
||||||
|
que.Enqueue(node.left);
|
||||||
|
if (node.right != null)
|
||||||
|
que.Enqueue(node.right);
|
||||||
|
if (node.left == null && node.right == null)
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<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">
|
||||||
|
@ -867,6 +867,31 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public int CountNodes(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
var left = root.left;
|
||||||
|
var right = root.right;
|
||||||
|
int leftDepth = 0, rightDepth = 0;
|
||||||
|
while (left != null)
|
||||||
|
{
|
||||||
|
left = left.left;
|
||||||
|
leftDepth++;
|
||||||
|
}
|
||||||
|
while (right != null)
|
||||||
|
{
|
||||||
|
right = right.right;
|
||||||
|
rightDepth++;
|
||||||
|
}
|
||||||
|
if (leftDepth == rightDepth)
|
||||||
|
return (int)Math.Pow(2, leftDepth+1) - 1;
|
||||||
|
return CountNodes(root.left) + CountNodes(root.right) + 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<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