java recursive solution


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

Log in to reply
 

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