/*
* @lc app=leetcode.cn id=101 lang=c
*
* [101] 对称二叉树
*
* https://leetcode-cn.com/problems/symmetric-tree/description/
*
* algorithms
* Easy (45.30%)
* Total Accepted: 23.8K
* Total Submissions: 52.4K
* Testcase Example: '[1,2,2,3,4,4,3]'
*
* 给定一个二叉树,检查它是否是镜像对称的。
*
* 例如,二叉树 [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
*
*
* 说明:
*
* 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
*
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool com(struct TreeNode* a,struct TreeNode* b); bool isSymmetric(struct TreeNode* root) { if(root == NULL) return true; return com(root->left,root->right);
}
bool com(struct TreeNode* a,struct TreeNode* b)
{
if(a == NULL&&b == NULL)
return true;
else
{
if(a == NULL||b == NULL)
return false;
else if(a -> val==b -> val)
return com(a->left,b->right)&&com(a->right,b->left);
else
return false;
}
}

这里应用递归算法。需要引用一个自己创建的函数(只有一个root是无法递归出来的)

其实有点像前一道相同的树那道题,这里判断的对称的本质就是 左子树的右子树等于右子树的左子树 就是对称。

----------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=101 lang=python3
#
# [101] 对称二叉树
#
# https://leetcode-cn.com/problems/symmetric-tree/description/
#
# algorithms
# Easy (45.30%)
# Total Accepted: 23.8K
# Total Submissions: 52.4K
# Testcase Example: '[1,2,2,3,4,4,3]'
#
# 给定一个二叉树,检查它是否是镜像对称的。
#
# 例如,二叉树 [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
#
#
# 说明:
#
# 如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
#
#
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root):
if root == None:
return True
else:
return self.isSym(root.left, root.right) def isSym(self, p, q):
if p == None and q == None:
return True
elif p == None or q == None:
return False
elif p.val == q.val:
return self.isSym(p.left, q.right) and self.isSym(p.right, q.left)
else:
return False

Leecode刷题之旅-C语言/python-101对称二叉树的更多相关文章

  1. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  2. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  3. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  4. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  8. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

  9. Leecode刷题之旅-C语言/python-349两整数之和

    /* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...

随机推荐

  1. SQLite入门操作(一)

    //++其他的头文件 #include "sqlite3.h" #pragma comment(lib,"sqlite3.lib") int GetItemCo ...

  2. rel 属性<small>H5保留属性</small>

    源文件

  3. jquery表单

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  4. 移动端调试工具weinre

    前段时间在大豹公众号上看到一个关于移动端调试的工具,了解了一下,确实不错. npm install -g weinre 先全局安装,然后使用的时候通过如下命令启动 weinre --boundHost ...

  5. Linux python2.4升级到2.7

    yum install gcc-c++  gcc -y mkdir /usr/local/python2.7tar zxvf Python-2.7.11.tgzcd Python-2.7.11/./c ...

  6. 3大框架Struts、Hibernate、Spring简单了解

    3大框架:Struts.Hibernate.Spring 基本概念:Spring/Struts/Hibernate是干嘛用的? 三个框架产生的技术历史背景 学习前首先应该掌握的基础知识 学习一个开发框 ...

  7. EntityFrameWork简单操作 EF数据上下文对象操作数据增删改差及批处理

    /// <summary> /// EF针对 留言数据库 的 数据上下文对象!!!! /// </summary> static LeaveWordBoradEntities ...

  8. 【洛谷P1582】倒水

    倒水 题目链接 显然,2^x个杯子里的水可以倒在一个杯子里 所以我们可以贪心地每次将N中最大的2^x减掉 减k次(若中途已经为0,直接输出0) 若大于0,用最小的比N大的2^x减剩下的N,即为答案 # ...

  9. Progress

    这个标签用来表示进度,常用来表示下载的进度. <progress value="22" max="100"></progress>   ...

  10. 与JSON相关的问题

    1.JSON.stringify 与 JSON.parse 相关的问题 JSON.stringify 把字符串转化为字符串,JSON.parse把字符串转化为JSON格式 会出现的问题Unexpect ...