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

    给定一个二叉树,检查它是否是镜像对称的。

    例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

        1
        /
       2 2
      / /
    3 4 4 3
     

    但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

        1
       /
      2 2
       
        3 3

    代码:

    bool isSymmetric(struct TreeNode* root){
        if (root == NULL) return true;
        return fun(root->left, root->right);
    }
    
    int fun(struct TreeNode* l_root, struct TreeNode* r_root)
    {
        if (l_root == NULL && r_root == NULL) return true;
        if (l_root == NULL || r_root == NULL) return false;
    
        return    (l_root->val == r_root->val)   &&
               fun(l_root->left,  r_root->right) &&
               fun(l_root->right, r_root->left);
    }
    

      

  • 相关阅读:
    内置函数二
    内置函数一
    lambda表达式
    函数参数
    set集合
    元组和字典的功能
    列表功能介绍
    分篮子
    松鼠配对?
    奇数次的数?
  • 原文地址:https://www.cnblogs.com/redzzy/p/13845566.html
Copyright © 2011-2022 走看看