BT6 二叉树的镜像
约 168 字小于 1 分钟
2025-04-18
描述
操作给定的二叉树,将其变换为源二叉树的镜像。
链接
示例
{8,6,10,5,7,9,11} => {8,10,6,11,9,7,5}
{5} => {5}
{} => {}题解
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
function Mirror(pRoot) {
// write code here
function go(root) {
if (root) {
const left = root.left;
const right = root.right;
if (left) go(left);
if (right) go(right);
root.left = right;
root.right = left;
}
}
go(pRoot);
return pRoot;
}
module.exports = {
Mirror: Mirror,
};