当前位置:网站首页>Simulation volume leetcode [general] 1609 Parity tree
Simulation volume leetcode [general] 1609 Parity tree
2022-07-07 08:55:00 【Encounter simulation volume】
1609. Even tree
If a binary tree satisfies the following conditions , It can be called Even tree :
The subscript of the layer where the root node of the binary tree is located is 0 , The layer of the child node of the root is subscript 1 , The layer of the root's grandson is subscript 2 , And so on .
Even subscript The values of all nodes on the layer are p. Integers , From left to right Strictly increasing
Odd subscript The values of all nodes on the layer are accidentally Integers , From left to right Strictly decreasing
Give you the root node of the binary tree , If the binary tree is Even tree , Then return to true , Otherwise return to false .
Example 1:
Input :root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output :true
explain : The node values of each layer are :
0 layer :[1]
1 layer :[10,4]
2 layer :[3,7,9]
3 layer :[12,8,6,2]
because 0 Layer and the 2 The node values on the layer are odd and strictly increasing , and 1 Layer and the 3 The node values on the layer are even and strictly decreasing , So this is a parity tree .
Example 2:
Input :root = [5,4,2,3,3,7]
Output :false
explain : The node values of each layer are :
0 layer :[5]
1 layer :[4,2]
2 layer :[3,3,7]
2 The node value on the layer does not satisfy the condition of strict increment , So it's not a parity tree .
Example 3:
Input :root = [5,9,1,3,5,7]
Output :false
explain :1 The node value on the layer should be even .
Example 4:
Input :root = [1]
Output :true
Example 5:
Input :root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17]
Output :true
Tips :
The number of nodes in the tree is in the range [1, 105] Inside
1 <= Node.val <= 106
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/even-odd-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
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 turn 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')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- Greenplum 6.x build_ Environment configuration
- ncs成都新电面试经验
- Led analog and digital dimming
- Unity shader beginner's Essentials (I) -- basic lighting notes
- Opencv converts 16 bit image data to 8 bits and 8 to 16
- oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
- Greenplum 6.x build_ install
- JS operation
- NCS Chengdu New Electric interview Experience
- 实现自定义内存分配器
猜你喜欢
Oracle makes it clear at one time that a field with multiple separators will be split into multiple rows, and then multiple rows and columns. Multiple separators will be split into multiple rows, and
Other 7 features of TCP [sliding window mechanism ▲]
LeetCode 715. Range module
数字三角形模型 AcWing 275. 传纸条
JS的操作
Led analog and digital dimming
Greenplum6.x重新初始化
[Yugong series] February 2022 U3D full stack class 005 unity engine view
使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题
2022-06-30 Unity核心8——模型导入
随机推荐
为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
如何在图片的目标中添加目标的mask
Sign and authenticate API interface or H5 interface
Original collection of hardware bear (updated on June 2022)
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
Speaking of a software entrepreneurship project, is there anyone willing to invest?
个人力扣题目分类记录
systemd
数据库存储---表分区
OpenGL frame buffer
MySQL partition explanation and operation statement
[Nanjing University] - [software analysis] course learning notes (I) -introduction
Test pits - what test points should be paid attention to when adding fields to existing interfaces (or database tables)?
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
Synchronized underlying principle, volatile keyword analysis
Greenplum6.x监控软件搭建
LED模拟与数字调光
leetcode135. Distribute candy
Frequently Asked Coding Problems