zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Check for symmetric binary tree

    Given a binary tree, check whether it is a mirror of itself using both recursion and iterative approach.

    Examples:

    Input :   
        
         1
       /   
      2     2
     /    / 
    3   4 4   3
    
    Output : Symmetric
    
    Input :    
       
        1
       / 
      2   2
          
       3    3
    
    Output : Not Symmetric


     1 import java.util.ArrayList;
     2 import java.util.LinkedList;
     3 import java.util.Queue;
     4 
     5 public class SymmetricBt {
     6     public static boolean checkIfSymRecursion(TreeNode root) {
     7         return isSymRecursionHelper(root, root);
     8     }
     9     private static boolean isSymRecursionHelper(TreeNode n1, TreeNode n2) {
    10         if(n1 == null && n2 == null) {
    11             return true;
    12         }
    13         if(n1 != null && n2 != null && n1.val == n2.val) {
    14             return isSymRecursionHelper(n1.left, n2.right) && isSymRecursionHelper(n1.right, n2.left);
    15         }
    16         return false;
    17     }
    18     public static boolean checkIfSymIterative(TreeNode root) {
    19         Queue<TreeNode> queue = new LinkedList<TreeNode>();
    20         queue.add(root);
    21         
    22         while(!queue.isEmpty()) {
    23             if(!isPalindrome(queue)) {
    24                 return false;
    25             }
    26             int size = queue.size();
    27             for(int i = 0; i < size; i++) {
    28                 TreeNode curr = queue.poll();
    29                 if(curr != null) {
    30                     queue.add(curr.left);
    31                     queue.add(curr.right);
    32                 }
    33             }
    34         }
    35         return true;
    36     }
    37     private static boolean isPalindrome(Queue<TreeNode> queue) {
    38         ArrayList<TreeNode> list = new ArrayList<TreeNode>(queue);
    39         int i = 0, j = list.size() - 1;
    40         while(i < j) {
    41             TreeNode left = list.get(i);
    42             TreeNode right = list.get(j);
    43             if(left != null || right != null) {
    44                 if(left == null || right == null || left.val != right.val) {
    45                     return false;
    46                 }
    47             }
    48             i++;
    49             j--;
    50         }
    51         return true;
    52     }
    53     public static void main(String[] args) {
    54         TreeNode n1 = new TreeNode(1);
    55         TreeNode n2 = new TreeNode(2);
    56         TreeNode n3 = new TreeNode(2);
    57         TreeNode n4 = new TreeNode(3);
    58         TreeNode n5 = new TreeNode(4);
    59         TreeNode n6 = new TreeNode(4);
    60         TreeNode n7 = new TreeNode(3);
    61         n1.left = n2; n1.right = n3;
    62         n2.left = n4; n2.right = n5;
    63         n3.left = n6; n3.right = n7;
    64         
    65         System.out.println(checkIfSymIterative(n1));
    66         
    67         TreeNode n8 = new TreeNode(1);
    68         TreeNode n9 = new TreeNode(2);
    69         TreeNode n10 = new TreeNode(2);
    70         TreeNode n11 = new TreeNode(3);
    71         TreeNode n12 = new TreeNode(3);
    72         n8.left = n9; n8.right = n10;
    73         n9.right = n11;
    74         n10.right = n12;
    75         
    76         System.out.println(checkIfSymIterative(n8));        
    77     }
    78 }

    Solution 1. Recursion

    For two trees t1 and t2 to be mirror of each other, the following three conditions must be true 

    1. their root node's value must be the same.

    2. t1's left subtree must be mirror of t2's right subtree.

    3. t1's right subtree must be mirror of t2's left subtree.

    The base case is when t1 and t2 are both null, they are mirror of each other.

    Solution 2. Iterative using level order traversal

    There are 2 ways of doing this.

    1. do a regular level order traversal from left to right at each level, then check if all nodes of each level are palindromic.

    2. enqueue in the following order:

      left child of left subtree -> right child of right subtree -> right child of left subtree -> left child of right subtree.

        always dequeue the first 2 nodes from the queue and compare if they are the same.

    
    
    
  • 相关阅读:
    Redis(二) 扩展
    Redis(一)基础
    Java垃圾回收机制 入门
    freeregex-0.01 使用文档
    上传文件到阿里云linux服务器
    jQuery代码解释(基本语法)
    JQuery中使用FormData异步提交数据和提交文件
    jQuery获取data-*属性值
    jquery 中 $.map 的使用方法
    mysql创建表时反引号的作用
  • 原文地址:https://www.cnblogs.com/lz87/p/7478738.html
Copyright © 2011-2022 走看看