mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0513.找树左下方的值,添加C#递归版
This commit is contained in:
@ -684,6 +684,38 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
//递归
|
||||||
|
int maxDepth = -1;
|
||||||
|
int res = 0;
|
||||||
|
public int FindBottomLeftValue(TreeNode root)
|
||||||
|
{
|
||||||
|
Traversal(root, 0);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode root, int depth)
|
||||||
|
{
|
||||||
|
if (root.left == null && root.right == null)
|
||||||
|
{
|
||||||
|
if (depth > maxDepth)
|
||||||
|
{
|
||||||
|
maxDepth = depth;
|
||||||
|
res = root.val;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root.left != null)
|
||||||
|
{
|
||||||
|
Traversal(root.left, depth + 1);
|
||||||
|
}
|
||||||
|
if (root.right != null)
|
||||||
|
{
|
||||||
|
Traversal(root.right, depth + 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<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