zoukankan      html  css  js  c++  java
  • Symmetric Tree

    -----QUESTION-----

    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.

    -----SOLUTION-----

    class Solution {
    public:
        bool isSymmetric(TreeNode *root) {
            if(!root) return true;
            
            bool result;
            if((root->left==NULL && root->right != NULL) ||(root->left!=NULL && root->right == NULL))
            {
                return false;
            }
            else if(root->left == NULL && root->right == NULL)
            {
                return true;
            }
            else
            {
                result = cmp(root->left, root->right);
            }      
        }
        bool cmp(TreeNode * node1, TreeNode* node2)
        {
            int result1 = true;
            int result2 = true;
            if(node1->val!=node2->val) return false;
            else
            {
                if((node1->left==NULL && node2->right != NULL) ||
                (node1->left!=NULL && node2->right == NULL)||
                (node1->right!=NULL && node2->left == NULL)||
                (node1->right==NULL && node2->left != NULL))
                {
                    return false;
                }
                if((node1->left == NULL && node2->right == NULL)&&
                (node1->right == NULL && node2->left== NULL))
                {
                    return true;
                }
                if(node1->left != NULL)
                {
                    result1 = cmp(node1->left,node2->right);
                }
                if(node1->right != NULL)
                {
                    result2 = cmp(node1->right,node2->left);
                } 
                return (result1 && result2);
            }
        }     
    };
    

    非递归方法

    class Solution {
    public:
        bool isSymmetric(TreeNode *root) {
            // Note: The Solution object is instantiated only once and is reused by each test case.
            if(root == NULL)    return true;
            queue<TreeNode*> q;
            q.push(root->left);
            q.push(root->right);
            TreeNode *t1, *t2;
            while(!q.empty()){
                t1 = q.front();
                q.pop();
                t2 = q.front();
                q.pop();
                if(t1 == NULL && t2 == NULL)
                    continue;
                if(t1 == NULL || t2 == NULL || t1->val != t2->val)
                    return false;
                q.push(t1->left);
                q.push(t2->right);
                q.push(t1->right);
                q.push(t2->left);
            }
            return true;
        }
    };

  • 相关阅读:
    《动手能力强与技术水平低》(2009/12/14)
    《为什么程序员被喻为“IT农民工”》(2009/12/12)
    对于关键字Ref和Out的理解
    2008世界500强排名(1100位)
    Javascript取select的选中值和文本
    使用 FreeNAS 下載 eMule
    程序员35岁后的三条出路
    二维条码 QR Code
    笔记本CPU性能排行榜Comparison of Mobile Processors (CPU Benchmarks)
    如何在C#中播放AVI短片并使背景透明
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3918100.html
Copyright © 2011-2022 走看看