题意描述:
请完成一个函数,输入一个二叉树,该函数输出它的镜像。0 <= 节点个数 <= 1000
例如输入:
4
/ \
2 7
/ \ / \
1 3 6 9
镜像输出:
4
/ \
7 2
/ \ / \
9 6 3 1
示例:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
解题思路:
Alice:我觉得这题根本就不是在考什么镜像,镜像的问题很好办,交换 left 和 right 的指向就好了,对每个节点都这样做一下就好了。
Bob: 对啊,难道说 ?
Alice: 这题根本就是在考怎样遍历一棵二叉树,比如说,先序遍历啦,层序遍历啦,递归的写法,非递归的写法啦。
Bob: 是啊,非递归的先序遍历我之前就遇到过这个问题 !!
Alice: 所以说你啊,补题很重要的,像这种题目,你不是应该背下来了吗 ?
Bob: 😥😥
代码:
Python 方法四: 手速版
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if root == None:
return None
else:
root.left, root.right = self.mirrorTree(root.right), self.mirrorTree(root.left)
return root
Java: 递归 + 先序遍历 + 反转
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mirrorTree(TreeNode root) {
helper(root);
return root;
}
public void helper(TreeNode root){
if(root == null){
return;
}else{
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
helper(root.left);
helper(root.right);
return;
}
}
}
Java 方法二: 队列 + 层序遍历 + 反转
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mirrorTree(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(queue.isEmpty() == false){
TreeNode node = queue.poll();
if(node != null){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
}
return root;
}
}
Java 方法三 : 栈 + 非递归先序遍历二叉树 + 反转
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root == null){
return root;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(stack.empty() == false){
TreeNode node = stack.pop();
if(node != null){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
// System.out.println(node.val);
if(node.right != null){
stack.push(node.right);
}
if(node.left != null){
stack.push(node.left);
}
}
}
return root;
}
}
Python 递归:先序遍历 + 反转
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
self.helper(root)
return root
def helper(self, root):
if root == None:
return
else:
root.left, root.right = root.right, root.left
self.mirrorTree(root.left)
self.mirrorTree(root.right)
Python 方法二: 队列 + 层序遍历 + 反转
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
queue = [root]
while len(queue) != 0:
node = queue.pop(0)
if node:
node.left, node.right = node.right, node.left
queue.append(node.left)
queue.append(node.right)
return root
Python 方法三: 栈 + 非递归先序遍历 + 反转
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
stack = [root]
while len(stack) != 0:
node = stack.pop(-1)
if node != None:
#print(node.val)
node.left, node.right = node.right, node.left
if node.right:
stack.append(node.right)
if node.left:
stack.append(node.left)
return root
JavaScript 非递归后续遍历二叉树
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var mirrorTree = function(root) {
if(root == null){
return root;
}else{
let stack = [];
let lastVisit = null;
let node = root;
//let array = [];
while(stack.length || node){
if(node){
stack.push(node);
node = node.left;
}else{
node = stack.pop();
if(node.right == null || node.right == lastVisit){
//array.push(node.val);
swap(node);
lastVisit = node;
node = null;
}else{
stack.push(node);
node = node.right;
}
}
}
//console.log(array);
return root;
}
};
var swap = function(root){
let tmp = root.left;
root.left = root.right;
root.right = tmp;
return;
}
JavaScript 非递归中序遍历二叉树
/**
非递归中序遍历二叉树
*/
var mirrorTree = function(root) {
if(root == null){
return root;
}else{
let stack = [];
let node = root;
//let array = [];
while(stack.length || node){
while(node != null){
stack.push(node);
node = node.left;
}
node = stack.pop();
//array.push(node.val);
swap(node);
node = node.left; // 反转后的中序遍历
// node = node.right; 正常的中序遍历
}
//console.log(array);
return root;
}
};
var swap = function(node){
let tmp = node.left;
node.left = node.right;
node.right = tmp;
return;
}
易错点:
- 一些测试点:
[4,2,7,1,3,6,9]
[1,2,3,4]
[1,2,3]
[1]
[]
总结: