Easy Java Solution


  • 0
    D
    class Solution
    {
    	List<String> paths = new ArrayList<String>();
    	public List<String> binaryTreePaths(TreeNode root)
    	{
    		if(root==null) return paths;
    		TPaths(root,root.val+"");
                    return paths;
            }
    	public void TPaths(TreeNode root, String s)
    	{
    		if(root.left==null && root.right==null)
    		{
    			paths.add(s);
    		}
    		if(root.left!=null)
    		{
    			TPaths(root.left,s+"->"+root.left.val);
    		}
    		if(root.right!=null)
    		{
    			TPaths(root.right,s+"->"+root.right.val);
    		}
    	}
    	
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.