zoukankan      html  css  js  c++  java
  • 对称二叉树

    WA1

    测试案例:192/193通过率。。。。

    /**
     * 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:
        std::vector<int>inorder1;
        std::vector<int>inorder2;
        bool isSymmetric(TreeNode* root) {
            if(root==NULL)return true;
            TreeNode* root2 = root;
            in1(root);
            in2(root2);
            bool ans = true;
            for(int i = 0;i<inorder1.size();i++){
                if(inorder1[i]!=inorder2[i]){
                    ans = false;
                    break;
                }
            }
        return ans;
    }
        void in1(TreeNode *root) {
            if (root->left)
                in1(root->left);
            else
                inorder1.push_back(-INT_MAX);
            inorder1.push_back(root->val);
            if (root->right)
                in1(root->right);
            else
                inorder1.push_back(INT_MAX);
        }
        void in2(TreeNode *root) {
            if (root->right)
                in2(root->right);
            else
                inorder2.push_back(-INT_MAX);
            inorder2.push_back(root->val);
            if (root->left)
                in2(root->left);
            else
                inorder2.push_back(INT_MAX);
        }
    };
    

    WA2

    122 / 193 个通过测试用例

    bool isSymmetric(TreeNode *root) {
        if(root==NULL)
            return true;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            int size = q.size();
            vector<int> subqueue;
            for (int i = 0; i < size; i++) {
                TreeNode* node = q.front();
                subqueue.push_back(node->val);
                q.pop();
                if(node->left)
                    q.push(node->left);
                else
                    subqueue.push_back(INT_MAX);
                if(node->right)
                    q.push(node->right);
                else
                    subqueue.push_back(-INT_MAX);
            }
            vector<int> subqueue2(subqueue);
            reverse(subqueue2.begin(),subqueue2.end());
            for (int j = 0; j < subqueue.size(); ++j) {
                if(subqueue[j]!=subqueue2[j])
                    return false;
            }
        }
        return true;
    

    AC1

        bool isSymmetric(TreeNode* root) {
           return isSymmetric2(root,root);
        }
        bool isSymmetric2(TreeNode* root1,TreeNode* root2){
          if(root1==NULL&&root2==NULL)return true;
          if(root2==NULL||root1==NULL)return false;
          return (root1->val==root2->val)&&isSymmetric2(root1->left,root2->right)&&isSymmetric2(root1->right,root2->left);
        }
    
    
  • 相关阅读:
    wdcp升级php和mysql
    centos安装中文支持(转)
    centos5.5用phpstudy一键安装配置虚拟主机后,yum配置代理服务器squid
    http status 汇总
    关于html标签元素的data-*属性
    [分 享] PHPCMS V9 更换域名,附件地址无法批更新(更换变便)问题>解决方法!!
    svn服务器配置小记
    Camstar Portal modeling user guid --设置本地时间
    msdn webcast 下载地址整理
    mvc 项目下 webservice 程序无法运行
  • 原文地址:https://www.cnblogs.com/sunqiangstyle/p/10312267.html
Copyright © 2011-2022 走看看