zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    Python生成器表达式
    Python列表解析
    Python迭代器(Iterator)
    Python set 集合
    python eval 函数妙用
    Python字典 (dict)
    Python序列之元组 (tuple)
    Python序列之列表 (list)
    递归和反射
    常用标准库
  • 原文地址:https://www.cnblogs.com/727713-chuan/p/3315059.html
Copyright © 2011-2022 走看看