forked from udit034/LeetCode_Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.BinaryTreePaths.java
34 lines (28 loc) · 1020 Bytes
/
Solution.BinaryTreePaths.java
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void binaryTreePathsUtil( TreeNode root, String cur, HashSet< String > h )
{
if( root == null )
return;
if( root != null && root.left == null && root.right == null )
{
h.add( cur.equals("") ? String.valueOf( root.val ) : cur + "->" + root.val );
return;
}
binaryTreePathsUtil( root.left, cur.equals("") ? String.valueOf( root.val ) : cur + "->" + root.val, h );
binaryTreePathsUtil( root.right, cur.equals("") ? String.valueOf( root.val ) : cur + "->" + root.val, h );
}
public List<String> binaryTreePaths(TreeNode root) {
HashSet< String > h = new HashSet< String >();
binaryTreePathsUtil( root, "", h );
return new ArrayList< String >( h );
}
}