描述
操作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数 0 \le n \le 10000≤n≤1000 , 二叉树每个节点的值 0\le val \le 10000≤val≤1000
要求: 空间复杂度 O(n)O(n) 。本题也有原地操作,即空间复杂度 O(1)O(1) 的解法,时间复杂度 O(n)O(n)
比如:
源二叉树
镜像二叉树
思路: 交换子树的左右位置,就可以达到题目要求的效果
function Mirror( pRoot ) {
// write code here
function cd(node){
if(!node) return ;
node.temp = node.left;
node.left = node.right;
node.right = node.temp;
cd(node.left);
cd(node.right);
}
cd(pRoot);
return pRoot;
}
牛客刷题