EasyStackTreeDepth-First SearchBinary Tree

Binary Tree Inorder Traversal

LeetCode
2 approaches, code in all languages

You are given the root of a binary tree. Return the values of its nodes visited in inorder sequence.

An inorder traversal processes each subtree in the order left child, then the node itself, then right child. Applied recursively from the root, this rule produces a single flat list of every value in the tree.

For a binary search tree, this ordering happens to yield the values sorted ascending, but the traversal itself is defined purely by the left-node-right visiting rule and works for any binary tree.

Example 1

Input: root = [1,null,2,3]

Output: [1,3,2]

The root 1 has no left child, so 1 is emitted first. Its right child 2 has left child 3, so we descend, emit 3, then 2.

Example 2

Input: root = [1,2,3,4,5]

Output: [4,2,5,1,3]

Node 1 has left subtree rooted at 2 (children 4 and 5) and right child 3. Inorder of the left subtree is 4,2,5, then the root 1, then 3.

Constraints

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now