1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class { public List<String> binaryTreePaths(TreeNode root) { List<String> res = new LinkedList<>(); if (root == null) { return res; } if (root.left == null && root.right ==null) { res.add("" + root.val); } List<String> leftRes = binaryTreePaths(root.left); for (String s : leftRes) { res.add(root.val + "->" + s); } List<String> rightRes = binaryTreePaths(root.right); for (String s : rightRes) { res.add(root.val + "->" + s); } return res; } }
|
近期评论