zoukankan      html  css  js  c++  java
  • Symmetric Tree

    题目:

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

    For example, this binary tree is symmetric:

        1
       / 
      2   2
     /  / 
    3  4 4  3
    

    But the following is not:

        1
       / 
      2   2
          
       3    3
    

    Note:
    Bonus points if you could solve it both recursively and iteratively.

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


    OJ's Binary Tree Serialization:

    The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

    Here's an example:

       1
      / 
     2   3
        /
       4
        
         5
    
    The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

    题解

    判断左右子树是否对称。

    解法一:递归的解法:

     1 public boolean isSymmetricTree(TreeNode p,TreeNode q){
     2      if(p == null&&q == null)
     3         return true;
     4      if(p == null||q == null)
     5         return false;
     6      return (p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left);
     7 }
     8 
     9 public boolean isSymmetric(TreeNode root) {
    10     if(root==null) 
    11         return true;
    12         
    13     return isSymmetricTree(root.left,root.right);
    14 }

    解法二:非递归解法:

     1 public boolean isSymmetric(TreeNode root) {
     2     if(root == null)
     3         return true;
     4     if(root.left == null && root.right == null)
     5         return true;
     6     if(root.left == null || root.right == null)
     7         return false;
     8     LinkedList<TreeNode> q1 = new LinkedList<TreeNode>();
     9     LinkedList<TreeNode> q2 = new LinkedList<TreeNode>();
    10     q1.add(root.left);
    11     q2.add(root.right);
    12     while(!q1.isEmpty() && !q2.isEmpty()){
    13         TreeNode n1 = q1.poll();
    14         TreeNode n2 = q2.poll();
    15         
    16         if(n1.val != n2.val)
    17             return false;
    18         if((n1.left == null && n2.right != null) || (n1.left != null && n2.right == null))
    19             return false;
    20         if((n1.right == null && n2.left != null) || (n1.right != null && n2.left == null))
    21             return false;
    22         
    23         if(n1.left != null && n2.right != null){
    24             q1.add(n1.left);
    25             q2.add(n2.right);
    26         }
    27         
    28         if(n1.right != null && n2.left != null){
    29             q1.add(n1.right);
    30             q2.add(n2.left);
    31         }            
    32     }
    33     return true;
    34 }

    reference: 

    http://blog.csdn.net/linhuanmars/article/details/23072829

  • 相关阅读:
    HDU 1075 What Are You Talking About(字典树)
    HDU 1075 What Are You Talking About (stl之map映射)
    HDU 1247 Hat’s Words(字典树活用)
    字典树HihoCoder
    HDU 1277全文检索(字典树)
    HDU 3294 Girls' research(manachar模板题)
    HDU 3294 Girls' research(manachar模板题)
    HDU 4763 Theme Section(KMP灵活应用)
    Ordering Tasks UVA
    Abbott's Revenge UVA
  • 原文地址:https://www.cnblogs.com/hygeia/p/4695064.html
Copyright © 2011-2022 走看看