1. 题目描述
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
2. 题解:递归
/**
* 二叉树的中序遍历
* Author: wu-meng
* Time: 2024.12.10
* 中序遍历的顺序是 左子树 - 根节点 - 右子树
*/
public class Solution36 {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorder(root, result);
return result;
}
/**
* 递归函数
*/
private void inorder(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
// 先遍历左子树
inorder(root.left, result);
// 添加根节点
result.add(root.val);
// 最后遍历右子树
inorder(root.right, result);
}
}