当前位置:网站首页>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 !

原网站

版权声明
本文为[Encounter simulation volume]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070617362728.html