Preorder, Inorder and Postorder of a Binary Tree
Updated
Problem statement
You are given the root of a binary tree. Return three lists of its node values, in this order:
- Preorder: visit a node, then its left subtree, then its right subtree.
- Inorder: visit the left subtree, then the node, then the right subtree.
- 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]]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 = [[],[],[]]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]]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
0and500. -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)