257. Binary Tree Paths

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 1 3ms 28.17%

public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<String>();
        if(root == null){
            return list;
        }
        createPath(list, String.valueOf(root.val), root);
        return list;
    }
    private void createPath(List<String> list, String path, TreeNode root){
        if(root.left == null && root.right == null){
            list.add(path);
            return;
        }
        if(root.left != null){
            createPath(list, path + "->" + String.valueOf(root.left.val), root.left);
        }
        if(root.right != null){
            createPath(list, path + "->" + String.valueOf(root.right.val), root.right);
        }
    }
}


Solution2 my solution

5ms 5.68%

public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
       List<String> list = new LinkedList<String>();
		List<TreeNode> path = new LinkedList<TreeNode>();
		if (root == null) {
			return list;
		}
		findPath(list, path, root);
		return list;
    }
   public static void findPath(List<String> list, List<TreeNode> path, TreeNode root) {
		List<TreeNode> newPath = new ArrayList<TreeNode>();
		newPath.addAll(path);
		newPath.add(root);
		if (root.left == null && root.right == null) {
			String s = newPath.get(0).val + "";
			for (int i = 1; i < newPath.size() - 1; i++) {
				s += "->";
				s += newPath.get(i).val + "";
			}
			if (newPath.size() - 1 != 0) {
				s += "->" + newPath.get(newPath.size() - 1).val;
			}
			list.add(s);
		} else {
			if (root.left != null) {
				findPath(list, newPath, root.left);
			}
			if (root.right != null) {
				findPath(list, newPath, root.right);
			}
		}
	}
}


Solution 3 3ms 28.17%

public class Solution {
    public int[] levelVal = new int[1000];
    public void helper(List<String> res, TreeNode root, int level) {
        if (root == null) {
            return;
        }
        levelVal[level] = root.val;
        if (root.left == null && root.right == null) {
            addToRes(res, level);    
        }
        helper(res, root.left, level + 1);
        helper(res, root.right, level + 1);
    }

    public void addToRes(List<String> res, int level) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < level; i++) {
            sb.append(levelVal[i]);
            sb.append("->");
        }
        sb.append(levelVal[level]);
        res.add(sb.toString());
        return;
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<String>();
        if (root == null) {
            return res;
        }

        helper(res, root, 0);
        return res;
    }
}


solution4 similar to solution 1

public static List<String> binaryTreePaths4(TreeNode root) {  
	     List<Integer> one=new ArrayList<>();  
	     List<String> res=new ArrayList<>();  
	     if (root!=null) dfs(root,one,res);  
	     return res;  
	   }  
	   private static void dfs(TreeNode x, List<Integer> one, List<String> res) {  
	     one.add(x.val);  
	     if (x.left!=null) dfs(x.left,one,res);  
	     if (x.right!=null) dfs(x.right,one,res);  
	     if (x.left==null && x.right==null) {  
	       StringBuilder sb=new StringBuilder();  
	       for (int num: one) {  
	         sb.append(String.valueOf(num));  
	         sb.append("->");  
	       }  
	       res.add(sb.substring(0,sb.length()-2));  
	     }  
	     one.remove(one.size()-1);  
	   }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值