Geek

博客园 首页 新随笔 联系 订阅 管理

 

 

/**
 * 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) {
        return mirror(root);
    }
    TreeNode mirror(TreeNode root) {
        if(root == null) return root;
        TreeNode temp = root.left;
        root.left =  root.right;
        root.right = temp;
        mirror(root.left);
        mirror(root.right);
        return root;

    }
}

 

 

/**
 * 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;
        Deque<TreeNode> q = new LinkedList<>();
        q.push(root);
        TreeNode temp = null;
        while(q.isEmpty() == false) {
            TreeNode parent = q.poll();
            temp  = parent.left;
            parent.left = parent.right;
            parent.right = temp;
            if(parent.left!=null) {
                q.offer(parent.left);
            }
            if(parent.right!=null) {
                q.offer(parent.right);
            }
        }
        return root;


    }
    
}

 

posted on 2020-07-15 17:58  .geek  阅读(114)  评论(0编辑  收藏  举报