-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathBinaryTreePaths.java
44 lines (38 loc) · 1014 Bytes
/
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
35
36
37
38
39
40
41
42
43
44
/* (C) 2024 YourCompanyName */
package tree;
import java.util.ArrayList;
import java.util.List;
/**
* Created by gouthamvidyapradhan on 09/12/2017. Given a binary tree, return all root-to-leaf paths.
*
* <p>For example, given the following binary tree:
*
* <p>1 / \ 2 3 \ 5 All root-to-leaf paths are:
*
* <p>["1->2->5", "1->3"]
*/
public class BinaryTreePaths {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
new BinaryTreePaths().inorder(root, result, "");
return result;
}
private void inorder(TreeNode node, List<String> list, String path) {
if (node != null) {
if (node.left == null && node.right == null) {
list.add(path + node.val);
} else {
inorder(node.left, list, path + node.val + "->");
inorder(node.right, list, path + node.val + "->");
}
}
}
}