[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.

 

转载于:https://www.cnblogs.com/lz87/p/7337139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值