題目:操作給定的二叉樹(shù),將其變換為源二叉樹(shù)的鏡像吗蚌。
練習(xí)地址
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011
https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
參考答案
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public void Mirror(TreeNode root) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);
}
}
復(fù)雜度分析
- 時(shí)間復(fù)雜度:O(n)纯出。
- 空間復(fù)雜度:O(logn)敷燎。