1. 程式人生 > ><LeetCode OJ> 101. Symmetric Tree

<LeetCode OJ> 101. Symmetric Tree

tco solid ini popu pop 轉載 point check round

101. Symmetric Tree

My Submissions Total Accepted: 90196 Total Submissions: 273390 Difficulty: Easy

給定一顆二叉樹,檢查是否鏡像對稱(環繞中心對稱)

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.

Subscribe to see which companies asked this question

Show Tags



分析(下面答案有極少的案例未通過。是錯誤答案!留作分析與紀念):

思路首先:
中序遍歷二叉樹。再推斷遍歷結果的對稱性
以題目為樣例:中序結果3241423。推斷序列顯然對稱
以下那個不正確稱的樣例:23123,推斷序列顯然不正確稱

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {  
         if(root){    
            inorderTraversal(root->left);    
            result.push_back(root->val);    
            inorderTraversal(root->right);    
        } 
        return result;
    }    

    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;
        inorderTraversal(root);//獲取中序結果
        for(int i=0;i<result.size()/2;i++)//推斷序列對稱否
            if(result[i]!=result[result.size()-1-i])
                return false;
        return true;    
    }
private:    
    vector<int> result;    
};


經過一段時間的分析才發現:

1),對於二叉樹形狀太極端情況是無法分辨的,

2),那種本來不是對稱二叉樹可是他的遍歷序列因為數字太巧合卻是對稱的就不行了!

舉例情況1:他的中序遍歷為。121,顯然不正確稱

   1
         2
             1

舉例情況2:他的中序遍歷為,2222。可是顯然不正確稱

    2
   /   2   2
               2


錯誤答案截止



學習別人的答案:

遞歸,從根節點開始,推斷左節點的左子樹與右節點的右子樹,左節點的右子樹與右節點的左子樹是否相等就可以!

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        if (!root) return true;
        return helper(root->left, root->right);
    }
    //推斷節點的左右子樹是否對稱
    bool helper(TreeNode* leftnode, TreeNode* rightnode) {
        if (!leftnode && !rightnode) //左右子樹均為空
            return true;
        
        if (!leftnode || !rightnode) //單子樹
            return false;

        if (leftnode->val != rightnode->val) //左右子樹不相等
            return false;
        //左節點的左子樹與右節點的右子樹。左節點的右子樹與右節點的左子樹
        return helper(leftnode->left,rightnode->right) && helper(leftnode->right, rightnode->left); 
    }  
};


學習別人的叠代:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue<TreeNode*> queue;
        if(!root)
            return true;
        queue.push(root->left);
        queue.push(root->right);
        TreeNode *leftnode,*rightnode;


        while(!queue.empty())
        {
            leftnode = queue.front();
            queue.pop();
            rightnode = queue.front();
            queue.pop();
            
            if (!leftnode && !rightnode) //左右子樹均為空  
                continue; 
            if (!leftnode || !rightnode) //單子樹  
                return false;     
            if(leftnode->val!=rightnode->val)//不相等
                return false;
            queue.push(leftnode->right);
            queue.push(rightnode->left);
            queue.push(leftnode->left);
            queue.push(rightnode->right);
        }
        return true;
    }
};


小結:

哎.....


註:本博文為EbowTang原創,興許可能繼續更新本文。假設轉載,請務必復制本條信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50562771

原作者博客:http://blog.csdn.net/ebowtang


&lt;LeetCode OJ&gt; 101. Symmetric Tree