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.

    
    
    
  • 相关阅读:
    [数字证书] 怎么打开windows的数字证书管理器
    [RF] 安装好Robot Framework之后怎样让启动的界面后面不带命令行窗口,且图片以机器人显示
    [RF]怎样用Robot Framework写好Test Case?
    iptables的疑问
    centos6.5安装jenkins文档部署全过程
    haproxy+keepalived以及haproxy的原理特点
    rhel6.5安装ansible
    客户端执行rsync出现的错误
    LVS_DR 安装后无法转发真实服务器,但是配置其他方面都检查的没有问题了。就剩在realserver这边没有在lo口上绑定VIP了
    架构设计:负载均衡层设计方案(1)——负载场景和解决方式
  • 原文地址:https://www.cnblogs.com/lz87/p/7478738.html
Copyright © 2011-2022 走看看