当前位置:网站首页>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 !
边栏推荐
- 9c09730c0eea36d495c3ff6efe3708d8
- Gson转换实体类为json时报declares multiple JSON fields named
- 平台化,强链补链的一个支点
- Interpolation lookup (two methods)
- 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
- Lenovo hybrid cloud Lenovo xcloud: 4 major product lines +it service portal
- RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (5 x 5). Kernel size c
- Several methods of calculating the average value of two numbers
- 注解@ConfigurationProperties的三种使用场景
- go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
猜你喜欢
Panel display technology: LCD and OLED
UnityShader入门精要个人总结--基础篇(一)
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
Greenplum6.x重新初始化
【istio简介、架构、组件】
Led analog and digital dimming
Three series of BOM elements
A bug using module project in idea
【Istio Network CRD VirtualService、Envoyfilter】
oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量
随机推荐
2022-06-30 Unity核心8——模型导入
Pointer advanced, string function
Enterprise manager cannot connect to the database instance
Implement custom memory allocator
opencv 将16位图像数据转为8位、8转16
Calf problem
Personal deduction topic classification record
Shell script for changing the current folder and the file date under the folder
Greenplum 6.x common statements
Original collection of hardware bear (updated on June 2022)
面板显示技术:LCD与OLED
平台化,强链补链的一个支点
Unityshader introduction essentials personal summary -- Basic chapter (I)
Routing information protocol rip
Sign and authenticate API interface or H5 interface
Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
Category of IP address
Uniapp wechat applet monitoring network
Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
实现自定义内存分配器