Leecode 0094. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal
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]
Explanation:
Example 2:
Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]
Output: [4,2,6,5,7,1,3,9,8]
Explanation:
Example 3:
Input: root = []
Output: []
Example 4:
Input: root = [1]
Output: [1]
题目大意
给定一棵二叉树的根节点 root
,返回其节点值的中序遍历结果。中序遍历的顺序是「左子树 → 根节点 → 右子树」,遵循 “左 - 根 - 右” 的递归逻辑,且需按此顺序收集所有节点值。
解题思路
二叉树的中序遍历有两种经典实现方式:递归法和迭代法。递归法逻辑直观,迭代法则需借助栈模拟递归过程,两种方法均需遵循 “左 - 根 - 右” 的核心顺序。
1 | /** |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.