/*
* @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

最新文章

  1. intellij idea使用
  2. Java泛型-类型擦除
  3. PowerDesigner 15.2入门学习 二
  4. .NET学习记录2
  5. Freemarker中遍历List以及内置函数使用
  6. CELERY里,这个WARNING如何消除?
  7. java程序员菜鸟进阶(十五)linux基础入门(三)linux用户和组管理
  8. 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
  9. 用Html5/CSS3做Winform,一步一步教你搭建CefSharp开发环境(附JavaScript异步调用C#例子,及全部源代码)上
  10. vue中一个dom元素可以绑定多个事件?
  11. java操作properties配置文件
  12. jquery cdn bootstrap静态资源库问题
  13. maven跳过测试编译命令
  14. 第四次Scrum编码冲刺!!!!
  15. Light oj 1018 - Brush (IV) 状态压缩
  16. yarn upgrade
  17. BZOJ.2437.[NOI2011]兔兔与蛋蛋游戏(二分图博弈 匈牙利)
  18. Python中调用自然语言处理工具HanLP手记
  19. Windows死机的话,可能的一些猫病
  20. 搭建harbor仓库、LDAP认证

热门文章

  1. solidity语言13
  2. SQL Server ->> SET ANSI_PADDING对于字符串插入的影响
  3. C# 调用 c++ 非托管dll时wchar类型参数的乱码处理
  4. CSS z-index的用法
  5. May 28th 2017 Week 22nd Sunday
  6. Fiddler实现IOS手机抓取https报文
  7. ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
  8. 【[SDOI2008]洞穴勘测】
  9. C# 利用HttpWebRequest进行HTTPS的post请求的示例
  10. 学习Apache的mod rewrite、access写法