zoukankan      html  css  js  c++  java
  • LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
    For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
        1
       /
      2   2
     /   /
    3  4 4  3

    But the following [1,2,2,null,3,null,3] is not:
        1
       /
      2   2
         
       3    3
    Note:
    Bonus points if you could solve it both recursively and iteratively.

     1 /**
     2  * Definition for a binary tree node.
     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 isSymmetric(TreeNode* root) {
    13         if(root==nullptr)
    14             return true;
    15         return helper(root->left,root->right);
    16     }
    17     bool helper(TreeNode* left,TreeNode* right)
    18     {
    19         if(!left&&!right)
    20             return true;
    21         else if(!left||!right)
    22             return false;
    23         else if(left->val!=right->val)
    24             return false;
    25         else
    26             return helper(left->left,right->right)&&helper(left->right,right->left);
    27     }
    28 };
  • 相关阅读:
    链表
    Python中安装Requests库
    在vs中使用python
    CentOS添加windows引导
    c#创建windows服务
    SQLServer查询结果另存为csv格式中文乱码问题
    用svn管理GitHub项目
    Jquery操作select
    sqlserver2016安装
    信号处理函数陷阱:调用malloc导致死锁[转]
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8572544.html
Copyright © 2011-2022 走看看