码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode Symmetric Tree

时间:2017-05-21 13:48:34      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:int   data-   style   ret   tree   cpp   efi   struct   nod   

非递归解法

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
       stack <TreeNode*> stackLeft;
       stack <TreeNode*> stackRight;
       
       if(root == NULL ||(root && (!root->left) && (!root->right))){
           return true;
       }
       
       if((root->left && (!root->right))||(!root->left && root->right)){
           return false;
       }
       TreeNode *pLeft = root->left;
       TreeNode *pRight = root->right;
       while((pLeft||(!stackLeft.empty())) && (pRight ||(!stackRight.empty()))){
            while(pLeft && pRight){
               
                if(pLeft->val != pRight->val){
                    return false;
                }
                stackLeft.push(pLeft); 
                pLeft = pLeft->left;
                stackRight.push(pRight);
                pRight = pRight->right;
            }
            
            if(pLeft || pRight){
                return false;
            }
            
            pLeft = stackLeft.top();
            stackLeft.pop();
            pLeft = pLeft->right;
            
            pRight = stackRight.top();
            stackRight.pop();
            pRight = pRight->left;
       }
       
       if( (pLeft||(!stackLeft.empty())) || (pRight ||(!stackRight.empty()))){
           return false;
       }
       
       return true;
    }
};<pre name="code" class="cpp">


递归解法

/**
 * Definition for binary tree
 * 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 == NULL || (root && !root->left && !root->right)){
           return true;
       }
       
       if( (root->left && !root->right) || (!root->left && root->right) ) {
           return false;
       }
       
       return checkLeftAndRight(root->left, root->right);
    }
    
    bool checkLeftAndRight(TreeNode *pLeft, TreeNode *pRight){
        
        if(!pLeft && !pRight){
            return true;
        }
        
        if( (pLeft && !pRight) || (!pLeft && pRight)){
            return false;
        }
        
        if(pLeft->val == pRight->val){
            return checkLeftAndRight(pLeft->left, pRight->right) && checkLeftAndRight(pLeft->right, pRight->left);
        } else {
            return false;
        }
    }
};


Leetcode Symmetric Tree

标签:int   data-   style   ret   tree   cpp   efi   struct   nod   

原文地址:http://www.cnblogs.com/yxysuanfa/p/6884464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!