preorderTraversal recursion in Java


  • 0
    L

    public class Solution {
    public List<Integer> list = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {

          if(root==null)return list;
    
          list.add(new Integer(root.val));
    
          if(root.left!=null)  preorderTraversal(root.left);
     
          if(root.right!=null) 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.