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 ...
随机推荐
- SharePoint2010 HTTP Error 503. The service is unavailable 解决方法
1.更改系统管理员用户密码前提 因为更改系管理员用户密码会影响到 "SharePoint2010"正常运行,所在尽量不要更改系统管理员用户的密码, 必须更改密码的话,需要注意以两点 ...
- 关于Cookie 的HttpOnly属性(java/web操作cookie+Tomcat操作jsessionid)
关于Cookie的其它只是不在累述.本文主要讲讲自己在项目中遇到的cookie的HttpOnly属性问题 Cookie的HttpOnly属性说明 cookie的两个新的属性secure和Httponl ...
- 屏幕 z
private void FullScreen() //全屏 { SizeMode = 2; FormBorderStyle = FormBo ...
- ios端微信浏览器禁止上下滑动
在body里的第一个div容器中添加 position:fixed
- Linux下实现免密码登录
1.Linux下生成密钥 ssh-keygen的命令手册,通过”man ssh-keygen“命令: 通过命令”ssh-keygen -t rsa“ 生成之后会在用户的根目录生成一个 “.ssh”的文 ...
- 12C RAC 常用检查命令,持续总结中
grid: olsnodes -s列出集群中节点crsctl check cluster -all检查几圈状态crsctl check clustercrsctl check crs 检查当前节点sr ...
- 让免费版MarkdownPad2使用Pro版本的功能
让免费版MarkdownPad2使用Pro版本的功能 找到配置文件 启用Markdown扩展模式 Markdown处理器:Github风格 语法检查设置 自动保存设置 找到配置文件 一般在用户的数据目 ...
- IOS VFL语言(页面布局)
● 什么是VFL语言 ● VFL全称是Visual Format Language,翻译过来是“可视化格式语言” ● VFL是苹果公司为了简化Autolayout的编码而推出的抽象语言 VFL ...
- 设计模式——原型模式(Prototype Pattern)
原型模式:用原型实例制定创建对象的种类,并且通过拷贝这些原型创建新的对象. UML 图: 原型类: package com.cnblog.clarck; /** * 原型类 * * @author c ...
- 计算次数,POJ(1207)
题目链接:http://poj.org/problem?id=1207 #include <stdio.h> #include <algorithm> using namesp ...