Java simple recursion solution and beats 95% users


  • 0
    F
    private List<Integer> list;
    
    public List<Integer> inorderTraversal(TreeNode root) {
        list = new LinkedList<Integer>();
        if (root == null) return list; // case root = null
        
        inOrder(root);
        
        return list;
    }
    
    private void inOrder(TreeNode node) {
        if (node == null) return;
        inOrder(node.left);
        list.add(node.val);
        inOrder(node.right);
    }

Log in to reply
 

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