Loading problem...
Given the root node of a binary tree, return an array containing the values of all nodes visited in inorder sequence.
In an inorder traversal of a binary tree, nodes are visited following this pattern: first, recursively visit all nodes in the left subtree, then visit the current node, and finally recursively visit all nodes in the right subtree. This traversal order is particularly significant because when applied to a Binary Search Tree (BST), it produces nodes in ascending sorted order.
The traversal should begin from the root and systematically explore the tree structure, collecting node values in the precise order they are encountered during the inorder walk. If the tree is empty (root is null), return an empty array.
root = [1,null,2,3][1,3,2]The tree structure is: 1
2
/
3
Starting inorder traversal from root: visit left subtree (empty), visit root (1), then right subtree. In the right subtree rooted at 2: visit left (3), visit node (2). Final sequence: [1, 3, 2].
root = [1,2,3,4,5,null,8,null,null,6,7,9][4,2,6,5,7,1,3,9,8]For this larger tree, the inorder traversal visits all left descendants first, then the current node, then all right descendants. The complete sequence collects values in left-root-right order at every level.
root = [1][1]A single-node tree has no left or right subtree. The inorder traversal simply visits the root node, returning [1].
Constraints