1. 题目描述
2. 解题思路
(1)用栈来做把,记录前一个结点,然后先从左边开始遍历,然后交换,再到右边开始,重复遍历即可。
3. 代码实现
(1)
public class Solution {
public void Mirror(TreeNode root) {
if (root == null)
return;
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()){
while (root != null){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
stack.push(root);
root = root.left;
}
if (!stack.isEmpty()){
root = stack.pop();
root = root.right;
}
}
return;
}
}