ps:今天做一個簡單的
題目描述
操作給定的二叉樹,將其變換爲源二叉樹的鏡像。
輸入描述:
二叉樹的鏡像定義:源二叉樹
8
/ \
6 10
/ \ / \
5 7 9 11
鏡像二叉樹
8
/ \
10 6
/ \ / \
11 9 7 5
題目分析
這就是傳統的交換兩個變量的值嘛,即藉助第三塊空間完成交換。流程還是遞歸
代碼實現
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Mirror(root) {
// write code here
if (root === null) {
return null;
}
let tep;
//左右子樹互換
tep = root.left;
root.left = root.right;
root.right = tep;
//轉換左子樹
Mirror(root.left);
// 轉換右子樹
Mirror(root.right);
}