题目
思路
什么是中序遍历:www.bilibili.com/video/BV15k…
- 有子节点的,出左
- 无左自出
- 到右子节点
- 返回到根
题解
递归做法:
const res = [];
const inorder = (root) => {
if (root == null) {
return;
}
inorder(root.left); // 先递归左子树
res.push(root.val); // 将当前节点值推入res
inorder(root.right); // 再递归右子树
};
inorder(root);
return res;