Leecode刷题之旅-C语言/python-101对称二叉树
/*
* @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对称二叉树的更多相关文章
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
随机推荐
- BIEE入门(四)展现层
BIEE里最终面向最终用户(业务界面使用者的)叫做BIEE的Presentation Layer也即展现层,展现层的定义将是最终用户Web报表开发界面里能够看见的完全一样的样子,所以展现层一般将是以最 ...
- mysql在表的某一位置增加一列、删除一列、修改列名
如果想在一个已经建好的表中添加一列,可以用以下代码: ) not null; 这条语句会向已有的表中加入一列,这一列在表的最后一列位置.如果我们希望添加在指定的一列,可以用: ) not null a ...
- babel 的一些记录
babel 的作用是将高版本的代码转换成低版本的可支持的代码: 过程是 读取 source code 转换为语法树 -> 经过处理 -> 转换为 code: babel有preset和pl ...
- React学习笔记 - JSX简介
React Learn Note 2 React学习笔记(二) 标签(空格分隔): React JavaScript 一.JSX简介 像const element = <h1>Hello ...
- EM自动任务导致数据库缓慢
这两天客户抱怨数据库慢,查看性能什么的没发现什么异常.查看job发现一个内容为EMD_MAINTENANCE.EXECUTE_EM_DBMS_JOB_PROCS执行特别频繁.使用 EXEC DBMS_ ...
- 小组Scrum第一次冲刺
团队任务描述: 在确定完分组,并对于敏捷开发做了相应的了解之后,我们团队开始了第一次的冲刺.对于我们团队的第一次的 Scrum冲刺,我们团队开展了团队会议.首先,我们明确了我们的目标,对其进行了相对初 ...
- TSP 遗传算法
GA——遗传算法 同模拟退火算法一样,都是现代优化算法之一.模拟退火是在一定接受程度的情况下仍然接受一个比较差的解. 遗传算法,是真真正正的和大自然的遗传进化有着非常紧密的联系的,当然遗传进化的只是在 ...
- Nmap的基础知识
扫描单一的一个主机: #nmap domain.com #nmap 192.168.1.2 扫描整个子网: #nmap 扫描多个目标: #nmap 192.168.1.2 192.168.1.5 查看 ...
- Ubuntu 16.04 安装 IDEA
1.下载地址:https://www.jetbrains.com/idea/download/#section=linux 选择without jdk版本下载 2.下载完成 解压 到 /opt下 先却 ...
- 不能安装这个“安装 OS X EI Capitan”应用程序副本不能验证” 的解决办法
不能安装这个“安装 OS X EI Capitan”应用程序副本不能验证.它在下载过程中可能已遭破坏或篡改. 解决办法:修改系统时间. 1,查看你的dmg文件的创建日期 2,把你的系统日期修改到比这个 ...