JZ27 二叉树的镜像
二叉树的镜像_牛客题霸_牛客网 (nowcoder.com)
//自顶向下递归
import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode root) {
if(root==null){
return null;
}
TreeNode tmp=root.left;
root.left=root.right;
root.right=tmp;
Mirror(root.left);
Mirror(root.right);
return root;
}
}
// //dfs-栈
// import java.util.*;
// public class Solution {
// public TreeNode Mirror (TreeNode root) {
// if(root==null){
// return null;
// }
// Stack<TreeNode> st=new Stack<>();
// st.push(root);
// while(st.isEmpty()==false){
// TreeNode cur=st.pop();
// if(cur.left!=null){
// st.push(cur.left);
// }
// if(cur.right!=null){
// st.push(cur.right);
// }
// TreeNode tmp=cur.left;
// cur.left=cur.right;
// cur.right=tmp;
// }
// return root;
// }
// }