BM33 二叉树的镜像

题目描述

image
image

思路分析

采用递归的方法,对每一个节点做相同的处理,交换节点位置,也就类似于我们交换两个变量的值一样,需要借助一个临时变量。
递归:
- 传递过来的节点需要做什么
- 递归退出条件
- 之后如何递归

代码参考

const Mirror = function (root) {
  if (!root) return
  const left = root.left
  root.left = root.right
  root.right = left
  Mirror(root.left)
  Mirror(root.right)
  return root
}
posted @ 2023-01-03 09:59  含若飞  阅读(13)  评论(0编辑  收藏  举报