/*
* @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. C#发送电子邮件 (异步) z

    ///验证电子邮件的正则表达式   string emailStr = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([ ...

  2. 【Leetcode】【Easy】Plus One

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  3. Tomcat与MySQL的数据源连接方法

    Tomcat配置数据源,由于项目经常访问数据库,需要不断地打开关闭,这就耗费了大量的资源.所以用数据源的方式访问数据库. 大体步骤: 配置server.xml 配置项目所在的WebRoot/WEB-I ...

  4. nutz 结合QueryResult,Record 自定义分页查询,不构建pojo 整合

    public QueryResult getHistoryIncome(int d, int curPage) throws Exception { /**sql**/ Sql sql = Sqls. ...

  5. POJ-3484 Showstopper---二分+前缀和

    题目链接: https://cn.vjudge.net/problem/POJ-3484 题目大意: 给出一系列等差数列,给出第一项和最后一项和公差 这些等差数列中每个数出现的次数只有一个是奇数,找出 ...

  6. BAT的云

    近期,关于用国内那家云非常纠结! 我也来说道说道各家云. 首先,说说我想要的云服务(按优先级): 0.最好能提供二级域名.移动互联网时代,顶级域名必需要吗?在手机浏览器上输入长长的域名非常蛋痛(即不要 ...

  7. Python:函数的命名空间、作用域与闭合函数

    1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...

  8. 【[SCOI2012]喵星球上的点名】

    好题啊 \(SA+ST\text{表}+\text{莫队}\) 我们先强行把所有的串连起来,串与串之间插入特殊字符,姓和名之间也插入特殊字符 之后跑一遍\(SA\),求出\(sa\)和\(het\) ...

  9. Maven Dependencies missing jar 解决

    在导入SVN项目之后发现Maven里面的pom.xml报错. 发现是Maven Dependencies 里面的jar包不完整. 我试过手动加入jar,但是不能成功,然后就又试着添加dependecn ...

  10. Android学习笔记_63_手机安全卫士知识点归纳(3)分享 程序锁 服务 进程管理 widget

    1.分享: Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setT ...