Update 0513.找树左下方的值,添加C#递归版
This commit is contained in:
parent
34de1fd8b0
commit
b1d56c8cff
|
|
@ -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">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue