当前位置:网站首页>模拟卷Leetcode【普通】1609. 奇偶树
模拟卷Leetcode【普通】1609. 奇偶树
2022-07-07 06:18:00 【邂逅模拟卷】
1609. 奇偶树
如果一棵二叉树满足下述几个条件,则可以称为 奇偶树 :
二叉树根节点所在层下标为 0 ,根的子节点所在层下标为 1 ,根的孙节点所在层下标为 2 ,依此类推。
偶数下标 层上的所有节点的值都是 奇 整数,从左到右按顺序 严格递增
奇数下标 层上的所有节点的值都是 偶 整数,从左到右按顺序 严格递减
给你二叉树的根节点,如果二叉树为 奇偶树 ,则返回 true ,否则返回 false 。
示例 1:
输入:root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
输出:true
解释:每一层的节点值分别是:
0 层:[1]
1 层:[10,4]
2 层:[3,7,9]
3 层:[12,8,6,2]
由于 0 层和 2 层上的节点值都是奇数且严格递增,而 1 层和 3 层上的节点值都是偶数且严格递减,因此这是一棵奇偶树。
示例 2:
输入:root = [5,4,2,3,3,7]
输出:false
解释:每一层的节点值分别是:
0 层:[5]
1 层:[4,2]
2 层:[3,3,7]
2 层上的节点值不满足严格递增的条件,所以这不是一棵奇偶树。
示例 3:
输入:root = [5,9,1,3,5,7]
输出:false
解释:1 层上的节点值应为偶数。
示例 4:
输入:root = [1]
输出:true
示例 5:
输入:root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17]
输出:true
提示:
树中节点数在范围 [1, 105] 内
1 <= Node.val <= 106
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/even-odd-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
from leetcode_python.utils import *
class Solution:
def __init__(self):
pass
def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
if not root:return True
isold = True
now_level = [root]
while now_level:
next_level = []
for i,now in enumerate(now_level):
if isold:
if 0==now.val&1: return False
if i>0 and now.val<=now_level[i-1].val:return False
else:
if now.val&1:return False
if i>0 and now.val>=now_level[i-1].val:return False
if now.left:next_level.append(now.left)
if now.right:next_level.append(now.right)
now_level=next_level
isold=not isold
return True
def test(data_test):
s = Solution()
data = data_test # normal
# data = [list2node(data_test[0])] # list转node
return s.isEvenOddTree(List2Tree(*data))
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[[1,10,4,3,None,7,9,12,8,6,None,None,2]],
# [[5,4,2,3,3,7]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!
边栏推荐
- Problems encountered in the use of go micro
- Greenplum6.x搭建_安装
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- 如何在图片的目标中添加目标的mask
- 年薪50w阿裏P8親自下場,教你如何從測試進階
- [Yugong series] February 2022 U3D full stack class 005 unity engine view
- MySQL主从延迟的解决方案
- Introduction to data fragmentation
- Pointer advanced, string function
- [wechat applet: cache operation]
猜你喜欢
![[step on the pit] Nacos registration has been connected to localhost:8848, no available server](/img/ee/ab4d62745929acec2f5ba57155b3fa.png)
[step on the pit] Nacos registration has been connected to localhost:8848, no available server

Novice entry SCM must understand those things

Input and output of floating point data (C language)

Greenplum6.x重新初始化

oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量

数字三角形模型 AcWing 1027. 方格取数
![[Nanjing University] - [software analysis] course learning notes (I) -introduction](/img/57/bf652b36389d2bf95388d2eb4772a1.png)
[Nanjing University] - [software analysis] course learning notes (I) -introduction

leetcode135. Distribute candy

Data type - integer (C language)

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
随机推荐
Required String parameter ‘XXX‘ is not present
Frequently Asked Coding Problems
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Rapid integration of authentication services - harmonyos platform
FPGA knowledge accumulation [6]
求有符号数的原码、反码和补码【C语言】
Three usage scenarios of annotation @configurationproperties
Database storage - table partition
Newly found yii2 excel processing plug-in
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
Greenplum6.x监控软件搭建
opencv之图像分割
关于基于kangle和EP面板使用CDN
[Chongqing Guangdong education] accounting reference materials of Nanjing University of Information Engineering
Quick sorting (detailed illustration of single way, double way, three way)
uniapp 微信小程序监测网络
idea里使用module项目的一个bug
POJ - 3784 running medium