当前位置:网站首页>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 !
边栏推荐
- Output all composite numbers between 6 and 1000
- OpenGL frame buffer
- Data analysis methodology and previous experience summary 2 [notes dry goods]
- C language for calculating the product of two matrices
- Golang etcdv3 reports an error. The attribute in grpc does not exist
- 年薪50w阿裏P8親自下場,教你如何從測試進階
- Digital triangle model acwing 275 Pass a note
- Enterprise manager cannot connect to the database instance
- 模拟卷Leetcode【普通】1706. 球会落何处
- 硬核分享:硬件工程师常用工具包
猜你喜欢
随机推荐
Image segmentation in opencv
Greenplum6.x搭建_安装
systemd
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
Golang etcdv3 reports an error. The attribute in grpc does not exist
Sign and authenticate API interface or H5 interface
Category of IP address
Required String parameter ‘XXX‘ is not present
C language for calculating the product of two matrices
Mock. JS usage details
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
Qt Charts使用(重写QChartView,实现一些自定义功能)
OpenGL 3D graphics rendering
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
opencv 将16位图像数据转为8位、8转16
Several methods of calculating the average value of two numbers
[南京大学]-[软件分析]课程学习笔记(一)-introduction
数据分片介绍
模拟卷Leetcode【普通】1706. 球会落何处