My AC easy solution


  • 0
    public List<String> binaryTreePaths(TreeNode root) {
            List<String> res = new ArrayList<String>();
            if (root == null) return res;
            DFS(root, "", res);
            return res;
        }
        
        private void DFS(TreeNode root, String s, List<String> res) {
            if (root == null) return;
            s = s + root.val;
            if (root.left == null && root.right == null)
                res.add(s);
            if (root.left != null)
                DFS(root.left, s + "->", res);
            if (root.right != null)
                DFS(root.right, s + "->", res);
            s.substring(0, s.length()-1);
        }
    

Log in to reply
 

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