Ugly but pass,i don't know which algorithm i should call it


  • 0
    M

    /**

    • 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) {
      ArrayList<String> list_s = new ArrayList<String>();
      String s = "";
      if(root == null)
      return list_s;

       helper(root,s,list_s);
       return list_s;
      

      }
      public void helper(TreeNode root,String s,List<String> list_s){
      if(root.left == null&& root.right == null)
      {
      list_s.add(s+root.val);
      return;
      }
      else if(root.left!= null&&root.right!= null)
      {
      String s1;
      s1 = s;
      helper(root.left,s+root.val+"->",list_s);
      helper(root.right,s1+root.val+"->",list_s);
      }
      else if(root.left!= null)
      {
      helper(root.left,s+root.val+"->",list_s);
      }
      else
      {
      helper(root.right,s+root.val+"->",list_s);
      }

      }
      }


Log in to reply
 

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