# 【LeetCode】257. Binary Tree Paths 解题报告

## Subject

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

1
/   \
2     3
\
5
All root-to-leaf paths are:

["1->2->5", "1->3"]

## Solution

### solution 1

public List<String> resultList = new ArrayList<String>();
    /**
* 递归方式
*
* <br />
*
* Run Time : 4ms
*
* @param root
* @return
*/
public List<String> binaryTreePaths(TreeNode root) {
if (root == null) {
return resultList;
}
List<String> singleResult = new ArrayList<>();

getTreePath(root, singleResult);
return resultList;
}
    /**
* DFS
*
* @param resultList
* @param node
* @param singleResult
*/
private void getTreePath(TreeNode node, List<String> singleResult) {
if (node.left == null && node.right == null) {
}
if (node.left != null) {
getTreePath(node.left, new ArrayList<>(singleResult));
}
if (node.right != null) {
getTreePath(node.right, new ArrayList<>(singleResult));
}
}

### solution 2

public List<String> binaryTreePaths2(TreeNode root) {
if (root == null) {
return resultList;
}
findPaths(root, root.val + "");
return resultList;
}

private void findPaths(TreeNode node, String path) {
if (node.left == null && node.right == null) {
}
if (node.left != null) {
findPaths(node.left, path + "->" + node.left.val);
}
if (node.right != null) {
findPaths(node.right, path + "->" + node.right.val);
}
}

### solution 3

so clever ~

public List<String> binaryTreePaths3(TreeNode root) {
findPath(root, new StringBuilder());
return resultList;
}

private void findPath(TreeNode node, StringBuilder sb) {
if (node == null) {
return;
}
int len = sb.length();
sb.append(node.val);
if (node.left == null && node.right == null) {
} else {
sb.append("->");
findPath(node.left, sb);
findPath(node.right, sb);
}
sb.setLength(len);// 截取 ！！！
}

### solution 4

public List<String> binaryTreePaths4(TreeNode root) {
if (root == null) {
return resultList;
}

Stack<Wrapper> stack = new Stack<>();
Wrapper wrapper = null;
while (!stack.isEmpty()) {
wrapper = stack.pop();
if (wrapper.node.left == null && wrapper.node.right == null) {
}
if (wrapper.node.left != null) {
+ wrapper.node.left.val));
}
if (wrapper.node.right != null) {
+ wrapper.node.right.val));
}
}

return resultList;
}

private static class Wrapper {
TreeNode node;
String path;

public Wrapper() {

}

public Wrapper(TreeNode node) {
this.node = node;
}

public Wrapper(TreeNode node, String path) {
this.node = node;
this.path = path;
}
}

### solution 5

    /**
* BFS
* with two Queue
* @param root
* @return
*/
public List<String> binaryTreePaths5(TreeNode root) {
if (root == null) {
return resultList;
}
nodeQueue.offer(root);
pathQueue.offer(root.val + "");
while (!nodeQueue.isEmpty()) {
TreeNode currNode = nodeQueue.poll();
String item = pathQueue.poll();
if (currNode.left == null && currNode.right == null) {
}
if (currNode.left != null) {
nodeQueue.offer(currNode.left);
pathQueue.offer(item + "->" + currNode.left.val);
}
if (currNode.right != null) {
nodeQueue.offer(currNode.right);
pathQueue.offer(item + "->" + currNode.right.val);
}
}

return resultList;
}

### solution 6

public List<String> binaryTreePaths6(TreeNode root) {
List<String> pathsList = new ArrayList<String>();

if (root == null) {
return pathsList;
}

if (root.left == null && root.right == null) {
return pathsList;
}

for (String path : binaryTreePaths6(root.left)) {
}

for (String path : binaryTreePaths6(root.right)) {
}

return pathsList;
}

yes.

https://github.com/crazy1235/BaseJavaProject/blob/master/BaseJavaProject/src/com/jacksen/java/leetcode/BinaryTreePaths.java

bingo~~