zoukankan      html  css  js  c++  java
  • Symmetric Tree

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isM(TreeNode *p,TreeNode *q)
    13     {
    14         if((p->left!=NULL)^(q->right!=NULL))
    15             return false;
    16         if((p->right!=NULL)^(q->left!=NULL))
    17             return false;
    18         if(p->val!=q->val)
    19             return false;
    20         if(p->left!=NULL&&(isM(p->left,q->right)==false))
    21             return false;
    22         if((p->right!=NULL)&&(isM(p->right,q->left)==false))
    23             return false;
    24         return true;
    25     }
    26     bool isSymmetric(TreeNode *root) {
    27         // Start typing your C/C++ solution below
    28         // DO NOT write int main() function
    29         if(root==NULL)
    30             return true;
    31         if(!(root->left||root->right))
    32             return true;
    33         if((root->left!=NULL)&&(root->right!=NULL))
    34         {
    35             return isM(root->left,root->right);
    36         }
    37         return false;
    38     }
    39 };
  • 相关阅读:
    记录我发现的第一个关于 Google 的 Bug
    iOS 中的 Delayed Transition
    Appstore|IPA
    地图|定位
    开发者账号
    App跳转
    国际化
    短信|彩信
    闪光灯
    Cornerstone|SVN
  • 原文地址:https://www.cnblogs.com/mengqingzhong/p/3073108.html
Copyright © 2011-2022 走看看