What is wrong with this code? Time limit exceeded


  • 0
    A

    class Solution {

        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> listpre = new ArrayList<Integer>();
            return preorder(root,listpre);
            
          
        }
        
        public List<Integer> preorder(TreeNode root,List<Integer> list){
        	TreeNode node =root;
            if (node==null) return list ;
            while(node!=null){
                list.add(new Integer(node.val));
                preorder(node.left,list);
                preorder(node.right,list);
                
            }
          return list;
            
        }
    

    }


Log in to reply
 

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