Skip to content

94. Binary Tree Inorder Traversal

Problem

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1:

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

Solve

You can return all the node value of a binary tree in many order: bfs, dfs, lnr, rnl, lrn .

The problem specifically require Inorder traversal, which mean left-node-right order

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        anwser = []
        if root is None:
            return anwser

        if root.left:
            anwser += self.inorderTraversal(root.left)
        anwser.append(root.val)
        if root.right:
            anwser += self.inorderTraversal(root.right)
        return anwser

Last update : August 17, 2023
Created : August 16, 2023