[leetcode] 1104. Path In Zigzag Labelled Binary Tree

Description

In an infinite binary tree where every node has two children, the nodes are labelled in row order.

In the odd numbered rows (ie., the first, third, fifth,…), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,…), the labelling is right to left.
Insert picture description here

Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.

Example 1:

Input: label = 14
Output: [1,3,4,14]

Example 2:

Input: label = 26
Output: [1,2,6,10,26]

Constraints:

  • 1 <= label <= 10^6

analysis

The meaning of the question is: find the path of a given node in the zigzag binary tree to the root node. The difficulty of this question lies in the formula derivation. If the position of each parent node can be calculated, it will be easy to solve the parent node recursively Click on it. How to calculate the position of the parent node, you can first calculate the number of the first node of each layer, that is, 2^level, and then calculate the number of the parent node to the end of the layer: (label-corner)// 2+1, since the number of the parent node is opposite to the number of the current node, the last number is corner-1-th+1. It may not be very clear. I wrote a code that can be debugged here. You can debug to feel the process.
For example, if n is 14, then its layer is 3, 2^3=8, and the number of nodes that differ from the parent node of the third layer to the fourth layer: th is (14-8)//2+1=4 , And its number is: corner-1-th+1=4. It is calculated like this.

Code

class Solution:
    def find_level(self,num):
        return math.floor(math.log(num,2))
    
    def find_parent(self,label):
        level=self.find_level(label)
        corner=2**level
        th=(label-corner)//2+1
        return corner-1-th+1
        
    def pathInZigZagTree(self, label: int) -> List[int]:
        res=[label]
        while(True):
            label=self.find_parent(label)
            if(label>=1):
                res.append(label)
            else:
                return res[::-1]

Code two

import math

class Solution:
    def __init__(self):
        super().__init__()
        self.corners=[]
        self.ths=[]
    def find_level(self,num):
        return math.floor(math.log(num,2))
    
    def find_parent(self,label):
        level=self.find_level(label)
        corner=2**level
        th=(label-corner)//2+1
        self.corners.append(corner)
        self.ths.append(th)
        return corner-1-th+1
        
    def pathInZigZagTree(self, label):
        res=[label]
        while(True):
            label=self.find_parent(label)
            if(label>=1):
                res.append(label)
            else:
                return res[::-1]
        

if __name__ == "__main__":
    solution=Solution()
    res=solution.pathInZigZagTree(14)
    print(res)
    print(solution.corners)
    print(solution.ths)
[1, 3, 4, 14]
[8, 4, 2, 1]
[4, 1, 1, 1]

references

[1].Runtime: 24 ms, faster than 91.89% python. https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/discuss/629982/Runtime%3A-24-ms-faster-than-91.89-python

Guess you like

Origin blog.csdn.net/w5688414/article/details/109272029