zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Convert a binary tree into its mirror tree

    Given a binary tree, convert it to its mirror tree.

    Mirror of a binary tree T is another binary tree M with the left and right children of all non-leaf nodes exchanged in T.

    Solution 1. Recursion, O(n) runtime

    Recursively get the mirror binary tree of the left child and right child of node N;

    Then reassign the old right child as N's left child; reassign the old left child as N's right child.

    Key note: We can easily forget about adding line 15 of saving the reference to root's left. 

    If this line is not there, this program does not work. It would get the mirror tree of root's right subtree,

    then reassign it as root's new left child. Then we try to get the mirror tree of root's old left subtree by calling 

    getMirrorBinaryTree(root.left). But root.left has been changed to the mirror of its original right subtree!

    We lost the original left subtree forever!

     1 class TreeNode{
     2     int key;
     3     TreeNode left, right;
     4     TreeNode(int key){
     5         this.key = key;
     6         this.left = null;
     7         this.right = null;
     8     }
     9 }
    10 public class Solution {
    11     public TreeNode getMirrorBinaryTree(TreeNode root){
    12         if(root == null){
    13             return null;
    14         }
    15         TreeNode temp = root.left;
    16         root.left = getMirrorBinaryTree(root.right);
    17         root.right = getMirrorBinaryTree(temp);
    18         return root;
    19     }
    20 }

    Solution 2. Iterative approach,  tree traversal

    Level order traversal to swap each non-leaf node's left and right child nodes.

     1 import java.util.LinkedList;
     2 import java.util.Queue;
     3 
     4 class TreeNode{
     5     int key;
     6     TreeNode left, right;
     7     TreeNode(int key){
     8         this.key = key;
     9         this.left = null;
    10         this.right = null;
    11     }
    12 }
    13 public class Solution {
    14     public TreeNode getMirrorBinaryTree(TreeNode root){
    15         if(root == null){
    16             return null;
    17         }
    18         Queue<TreeNode> queue = new LinkedList<TreeNode>();
    19         queue.offer(root);
    20         while(!queue.isEmpty()){
    21             TreeNode curr = queue.poll();
    22             if(curr.left == null && curr.right == null){
    23                 continue;
    24             }
    25             TreeNode temp = curr.left;
    26             curr.left = curr.right;
    27             curr.right = temp;
    28             if(curr.left != null){
    29                 queue.offer(curr.left);
    30             }
    31             if(curr.right != null){
    32                 queue.offer(curr.right);
    33             }
    34         }
    35         return root;
    36     }
    37 }

    Follow up question: do all tree traversal methods work for this problem? (pre order, in order, post order and level order)

    Yes.

    Pre-order: swap children nodes first, then change left subtree to its mirror, change right subtree last; Top down approach

     1 public class Solution {
     2      public void getMirrorBinaryTree(TreeNode root){
     3          if(root == null){
     4              return;
     5          }
     6          TreeNode temp = root.left;
     7          root.left = root.right;
     8          root.right = temp;
     9          getMirrorBinaryTree(root.left);
    10          getMirrorBinaryTree(root.right);
    11      }
    12 }

    In-order

    Both pre and post order solutions are pretty straightforward. In order is a bit tricky. 

    First get the mirror of root's left subtree; 

    Then we swap root's children nodes; 

    Get the mirror of root's left subtree again as this new left subtree is the old right subtree of root.

     1 public class Solution {
     2      public void getMirrorBinaryTree(TreeNode root){
     3          if(root == null){
     4              return;
     5          }
     6          getMirrorBinaryTree(root.left);
     7          TreeNode temp = root.left;
     8          root.left = root.right;
     9          root.right = temp;
    10          getMirrorBinaryTree(root.left);
    11      }
    12 }

    Post-order: change left subtree to its mirror, then right subtree to its mirror, swap children nodes last; Bottom up approach

    Solution 1 is actually a post order traversal. The following re-implementation makes the post order nature more clear.

    Essentially we need to recursively get the mirror tree of root node's left subtree and right subtree, then exchange root's

    left and right child nodes.

     1 public class Solution {
     2      public void getMirrorBinaryTree(TreeNode root){
     3          if(root == null){
     4              return;
     5          }
     6          getMirrorBinaryTree(root.left);
     7          getMirrorBinaryTree(root.right);
     8          TreeNode temp = root.left;
     9          root.left = root.right;
    10          root.right = temp;
    11      }
    12 }

    Challenge:

    Use iterative tree traversal to solve this problem.

  • 相关阅读:
    django 标签的使用
    django(models)视图与html 简单的操作
    Django 简单的使用
    安全性测试计划
    常用缩写
    ADB 基本命令整理
    kindle升级测试
    虚拟机的类加载机制
    垃圾收集器与内存分配策略之篇三:理解GC日志和垃圾收集器参数总结
    垃圾收集器与内存分配策略之篇二:垃圾收集器
  • 原文地址:https://www.cnblogs.com/lz87/p/7337139.html
Copyright © 2011-2022 走看看