翻转二叉树

LeetCode 226:递归交换左右子树(最基础的树 DFS)。

#resource / algorithm #type / snippet #status / evergreen #source / leetcode #ds / tree #algo / dfs #algo / recursion

[!info] related notes 算法面试题型 MOC 二叉树基础 DFS / BFS / 回溯

翻转二叉树

题目

226. 翻转二叉树 - 力扣(LeetCode)

思路

对每个节点做同一件事:交换它的左右孩子,然后递归处理左右子树。

代码(JavaScript)

var invertTree = function(root) {
  if (root === null) return null;
  const left = invertTree(root.left);
  const right = invertTree(root.right);
  root.left = right;
  root.right = left;
  return root;
};
创建于 2026/3/16 更新于 2026/5/27