解题思路
此时需要进行临时存储,方便向下递归可以同时进行。
相关代码
/**
* 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 null;
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
//必须要进行临时存储,因为节点不能同时进行向下递归。
//如果不进行临时存储,之后的root会不一样。
root.right = left;
root.left = right;
return root;
}
}