-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0257-Binary-tree-paths.cs
More file actions
37 lines (30 loc) · 933 Bytes
/
0257-Binary-tree-paths.cs
File metadata and controls
37 lines (30 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0257.Binary_tree_paths
{
public class _0257_Binary_tree_paths
{
public IList<string> BinaryTreePaths(TreeNode root)
{
List<string> res = new List<string>();
if (root == null) return null;
Path(root, string.Empty, res);
return res;
}
private void Path(TreeNode node, string currentPath, List<string> res)
{
currentPath += node.val + "->";
if (node.left == null && node.right == null)
{
res.Add(currentPath.Substring(0, currentPath.Length - 2));
return;
}
if (node.left != null)
Path(node.left, currentPath, res);
if (node.right != null)
Path(node.right, currentPath, res);
}
}
}