share my java clean solution


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

Log in to reply
 

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