Simple java Solution bets 75%!!!!


  • 0
    A
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> res = new ArrayList<String>();
            if (root == null){
                return res;
            }
            getPaths (root, "", res);
            return res;
        }
        
        public void getPaths(TreeNode root, String str, List<String> res){
            if (str.equals("")){
                str += root.val;
            }
            else {
                str += "->"+root.val;
            }
            if (root.left == null && root.right == null){
                res.add (str);
                return;
            }
            if (root.left != null){
                getPaths (root.left, str, res);
            }
            if (root.right != null){
                getPaths (root.right, str, res);
            }
        }
    }

  • 0
    Z

    It is not 75% now.Please change your title.thanks


Log in to reply
 

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