操做給定的二叉樹,將其變換爲源二叉樹的鏡像

//方案一:java

public class Solution {code

public void Mirror(TreeNode root) {
    if(root==null)
        return;
    TreeNode temp=root.left;
    root.left=root.right;
    root.right=temp;
    Mirror(root.left);
    Mirror(root.right);

}

}io

//方案二:class

import java.util.*;import

public class Solution {im

public void Mirror(TreeNode root) {
    if(root==null)
        return;

     Stack <TreeNode> stack = new Stack<TreeNode>();
    stack.push(root);

    while(!stack.isEmpty()){
         TreeNode cur=stack.pop();

		if(cur.left!=null||cur.right!=null){
             TreeNode temp=cur.left;
             cur.left=cur.right;
             cur.right=temp;
         }

         if(cur.left!=null)
             stack.push(cur.left);
         if(cur.right!=null)
             stack.push(cur.right);
    }

}

}while

相關文章
相關標籤/搜索