数据结构与算法--翻转二叉树

/**
 * 翻转二叉树
 * @param root
 * @returns {*}
 */
function revertTree(root) {
  if (root === null) return null;

  revertTree(root.left); // 翻转左子树
  revertTree(root.right); // 翻转右子树

  // 交换左右子树 swap(root.left, root.right)
  let tmp = root.left;
  root.left = root.right;
  root.right = tmp;
  return root;
}

猜你喜欢

转载自blog.csdn.net/u013234372/article/details/83343544