FastPrepPreorder, Inorder and Postorder of a Binary Tree
Problems/Preorder, Inorder and Postorder of a Binary Tree

Preorder, Inorder and Postorder of a Binary Tree

Capital One logoCapital One● EasyINTERNNEW GRADFULLTIMEOAONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Capital One reports, from 2017 and 2022, list a tree traversal coding question. Two of them say it was a binary tree. One is from an intern final round, one from a TDP final round, and one from a 2022 process whose first step was an algorithm problem done at home before the interview day. None of them says which traversal order was wanted or how the tree is given.

Here you return all three depth-first orders, preorder, inorder and postorder, in that order, for a tree given in level order. The 2022 report also mentions deleting nodes but gives no rule for it, so that part is not included. The tree size and value limits are our choice.

Our version is about 70% the same as the reported questions.

You are given the root of a binary tree. Return three lists of its node values, in this order:

  1. Preorder: visit a node, then its left subtree, then its right subtree.
  2. Inorder: visit the left subtree, then the node, then the right subtree.
  3. Postorder: visit the left subtree, then the right subtree, then the node.

Each subtree is visited with the same rule. If the tree is empty, return three empty lists.

Function

binaryTreeTraversals(root: TreeNode) → List<List<Integer>>

Examples

Example 1

root = [4,2,7,null,5,6]return = [[4,2,5,7,6],[2,5,4,6,7],[5,2,6,7,4]]
root
42576

The root is 4. Its left child is 2, which has only a right child 5. Its right child is 7, which has only a left child 6.

  • Preorder visits a node, then its left subtree, then its right subtree: 4, 2, 5, 7, 6.
  • Inorder visits the left subtree, then the node, then the right subtree: 2, 5, 4, 6, 7.
  • Postorder visits both subtrees before the node: 5, 2, 6, 7, 4.

Example 2

root = []return = [[],[],[]]
root
empty

An empty input means there is nothing to visit. All three answers come back as empty lists.

Example 3

root = [8,3,null,1]return = [[8,3,1],[1,3,8],[1,3,8]]
root
831

The tree is a path going left: 8, then its left child 3, then its left child 1. Preorder lists each node before the ones below it: 8, 3, 1. No node has a right child, so inorder and postorder both finish the left side first and give 1, 3, 8.

Constraints

  • The number of nodes in the tree is between 0 and 500.
  • -1000 <= Node.val <= 1000

More Capital One Online Assessment questions

  • Rank Football TeamsOA · Seen Oct 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
  • Arithmetic Triplets After QueriesOA · Seen Oct 2026
  • Branch with the Most FilesOA · Seen Oct 2026
  • Count One-Swap Number PairsOA · Seen Oct 2026
  • Distinct Values on Maximum-Sum FramesOA · Seen Oct 2026
  • Earliest Meeting Time for All EmployeesOA · Seen Oct 2026
  • Earn the Most 🐝OA · Seen Oct 2026
  • Sort Adjacent PairsOA · Seen Oct 2026
  • Bouncing Diagonal WeightsOA · Seen Oct 2026

Similar questions at other companies

  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • All Root-to-Leaf Paths in a Binary TreeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
  • Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions

Capital One interview experiences

  • Capital One Experienced Engineer Power Day and OA: 6 Experiences
  • Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
  • Capital One TDP Agentic Round (AI Coding): 7 Experiences
  • Capital One SWE Power Day, Level Not Reported: 6 Experiences
  • Capital One TIP Intern OA and Power Day: 13 Experiences
  • Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)
All Capital One interview questionsSee Capital One hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> binaryTreeTraversals(TreeNode root) {
        // Write your code here.
    }
}
Checking account…