zoukankan      html  css  js  c++  java
  • Leetcode Symmetric Tree

    非递归解法

    /**
     * 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;
            }
        }
    };


  • 相关阅读:
    File 类
    RocketMQ4.X 集群
    Java 的基本网络支持
    SpringBoot2.X 整合 RocketMQ4.X
    RocketMQ系列:rocketmq运维控制台使用详解(全网独家)
    RocketMQ系列:docker搭建rocketmq单机环境
    RocketMQ系列:rocketmq运维控制台搭建
    RocketMQ系列:单机快速搭建单broker环境
    Jenkins:Git拉取代码版本不对
    pip3:no acceptable C compiler found in $PATH
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6884464.html
Copyright © 2011-2022 走看看