-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreePaths.java
More file actions
39 lines (27 loc) · 1.01 KB
/
Copy pathBinaryTreePaths.java
File metadata and controls
39 lines (27 loc) · 1.01 KB
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
// Given a binary tree, return all root-to-leaf paths.
// https://leetcode.com/problems/binary-tree-paths/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.*;
import java.util.ArrayList;
import java.util.List;
import leetcode.util.tree.TreeNode;
public class BinaryTreePaths {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
helper(root, "", res);
return res;
}
private void helper(TreeNode root, String curr, List<String> result) {
if (root == null) return;
if (root.left == null && root.right == null) {
result.add(curr + root.val);
}
helper(root.left, curr + root.val + "->", result);
helper(root.right, curr + root.val + "->", result);
}
public static void main(String[] args) {
TreeNode t1 = initTree(1, 2, 3, null, 5);
// printInorder(t1);
System.out.println(new BinaryTreePaths().binaryTreePaths(t1));
}
}