• 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) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(root==NULL)
            return true;
            vector<TreeNode *> left;
            vector<TreeNode *> right;
            vector<TreeNode *> templ;
            vector<TreeNode *> tempr;
            templ.push_back(root);
            tempr.push_back(root);
            while(!templ.empty()&&!tempr.empty())
            {
                vector<TreeNode*> prel;
                vector<TreeNode*> prer;
                int size = tempr.size();
                for(int i = 0;i < size;i++)
                {
                    if(templ[i]->left != NULL)
                    {
                        if(tempr[i]->right == NULL)
                        return false;
                        else if(tempr[i]->right->val == templ[i]->left->val)
                        {
                            prer.push_back(tempr[i]->right);
                            prel.push_back(templ[i]->left);
                        }
                        else
                        {
                            return false;
                        }
                    }
                    if(templ[i]->right != NULL)
                    {
                        if(tempr[i]->left == NULL)
                        return false;
                        else if(templ[i]->right->val == tempr[i]->left->val)
                        {
                            prer.push_back(tempr[i]->left);
                            prel.push_back(templ[i]->right);
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                templ = prel;
                tempr = prer;
            }
            return true;
        }
    };
  • 相关阅读:
    shell
    regionMatches方法
    ==
    使用INTO子句创建新表
    数据库除运算
    数据库笛卡尔积运算
    人生格言
    刚开通~
    Nginx:413 Request Entity Too Large
    ORACLE 查看并修改最大连接数
  • 原文地址:https://www.cnblogs.com/727713-chuan/p/3315059.html
Copyright © 2020-2023  润新知