Skip to content
CodeKruPro

Inorder traversal of a binary tree

Coding problemEasyAsked in: Deloitte

Given the root of a binary tree, return its values in inorder: first everything in the left subtree (in inorder), then the node itself, then everything in the right subtree (in inorder).

Trees are written in level order (the root, then its children from left to right, then their children, and so on), with null for a missing child. A subtree is a node together with everything below it. An empty tree gives an empty list. The hidden tests include trees 100,000 levels deep.

Examples

Example 1
Input: root = [1,null,2,3]
Output: [1,3,2]
1 has no left child and right child 2; 2 has left child 3.
Example 2
Input: root = [4,2,6,1,3,5,7]
Output: [1,2,3,4,5,6,7]
Example 3
Input: root = []
Output: []

Constraints

  • The tree has between 0 and 100,000 nodes
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.