6 lines simple java solution


  • 0
    N
    public class Solution {
    
    List<Integer> list = new ArrayList<>();
    
    public List<Integer> preorderTraversal(TreeNode root) {
        
        if(root==null) return list;
        
        list.add(root.val);
        
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        
        return list;
        
    }
    

    }


Log in to reply
 

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