Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9923f8

Browse files
committed
Update 0257.二叉树的所有路径,添加C#递归
1 parent eb0504e commit a9923f8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎problems/0257.二叉树的所有路径.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,43 @@ impl Solution {
900900
}
901901
}
902902
```
903+
### C#
904+
```C#
905+
public IList<string> BinaryTreePaths(TreeNode root)
906+
{
907+
List<int> path = new();
908+
List<string> res = new();
909+
if (root == null) return res;
910+
Traversal(root, path, res);
911+
return res;
912+
}
913+
public void Traversal(TreeNode node, List<int> path, List<string> res)
914+
{
915+
path.Add(node.val);
916+
if (node.left == null && node.right == null)
917+
{
918+
string sPath = "";
919+
for (int i = 0; i < path.Count - 1; i++)
920+
{
921+
sPath += path[i].ToString();
922+
sPath += "->";
923+
}
924+
sPath += path[path.Count - 1].ToString();
925+
res.Add(sPath);
926+
return;
927+
}
928+
if (node.left != null)
929+
{
930+
Traversal(node.left, path, res);
931+
path.RemoveAt(path.Count-1);
932+
}
933+
if (node.right != null)
934+
{
935+
Traversal(node.right, path, res);
936+
path.RemoveAt(path.Count-1);
937+
}
938+
}
939+
```
903940

904941
<p align="center">
905942
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /