-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0257-BinaryTreePaths.cs
52 lines (46 loc) · 1.52 KB
/
0257-BinaryTreePaths.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//-----------------------------------------------------------------------------
// Runtime: 248ms
// Memory Usage: 31.8 MB
// Link: https://leetcode.com/submissions/detail/344758530/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class _0257_BinaryTreePaths
{
public IList<string> BinaryTreePaths(TreeNode root)
{
if (root == null) return new List<string>();
var result = new List<IList<int>>();
GetPath(root, new List<int>(), result);
return result.Select(i => string.Join("->", i)).ToList();
}
public void GetPath(TreeNode node, IList<int> current, IList<IList<int>> result)
{
var next = new List<int>(current);
if (node == null) return;
next.Add(node.val);
if (node.left == null && node.right == null)
result.Add(next);
else
{
GetPath(node.left, next, result);
GetPath(node.right, next, result);
}
}
}
}