Skip to content

Commit

Permalink
BinaryTreePaths.java
Browse files Browse the repository at this point in the history
  • Loading branch information
terminator123 committed Jan 21, 2016
1 parent d07bb16 commit 2ae31d6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/BinaryTreePaths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Tree;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author chenqun
* Given a binary tree, return all root-to-leaf paths.
*
*/
public class BinaryTreePaths {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<String> ret = new ArrayList<String>();
public List<String> binaryTreePaths(TreeNode root){
List<Integer> list = new ArrayList<Integer>();
if(root == null) return ret;
dfs(root,list);
for(int i=0; i<ans.size(); i++){
String path = "";
int length = ans.get(i).size();
path = path + ans.get(i).get(0);
for(int j=1; j<length;j++)
path = path + "->" + ans.get(i).get(j);

ret.add(path);
}
return ret;
}
public void dfs(TreeNode root,List<Integer> list){
list.add(root.val);
if(root.left == null && root.right == null){
ans.add(new ArrayList(list));
list.remove(list.size()-1);
return;
}
if(root.left != null){
dfs(root.left,list);
}
if(root.right != null){
dfs(root.right,list);
}
list.remove(list.size()-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

0 comments on commit 2ae31d6

Please sign in to comment.