Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to verify the preorder serialization of python binary tree

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to verify the preorder serialization of the python binary tree, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. the content of the topic

One way to serialize a binary tree is to use preorder traversal. When we encounter a non-empty node, we can record the value of that node. If it is an empty node, we can use a tag value record, such as #.

_ 9_

/\

3 2

/ /

4 1 # 6

/ /

# #

For example, the above binary tree can be serialized into the string "9pr 3pj4", where # represents an empty node.

Given a comma-separated sequence, verify that it is the correct preorder serialization of the binary tree. Write a feasible algorithm without reconstructing the tree.

Each comma-separated character is either an integer or a'# 'that represents a null pointer.

You can think that the input format is always valid, for example, it will never contain two consecutive commas, such as "1thecomponent 3".

Example 1:

Enter: "9, 3, 4, 4, 1, 1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4

Output: true

Example 2:

Enter: "1Jing #"

Output: false

Example 3:

Enter: "9 minutes, 9 minutes, 1 years."

Output: false

Second, the way to solve the problem

Use the stack to store numbers, and'#'as the basis for getting out of the stack

Store a'#'in the stack beforehand

When the last one in the stack is'# 'and the last preorder sequence is' #', it means that the preorder serialization of the binary tree is correct.

Otherwise, if the'#'in preorder matches the'#'in the stack before it reaches the last position, it will be incorrect.

Code class Solution: def isValidSerialization (self, preorder: str)-> bool: stack = ['#'] I = 0 while I < len (preorder): if preorder [I] = =' ': I + = 1 continue elif preorder [I] =' #': if stack [- 1] = ='#': if I = = len (preorder)-1: stack.pop () else: return False Else: stack.pop () elif preorder [I]! ='#': stack.append (preorder [I]) while I < len (preorder) and preorder [I]! =' ': I + = 1 I + = 1 return len (stack) = = 0if _ _ name__ = =' _ _ main__': s = Solution () preorder = "9, main__':, 3, 4, 2, 6, 6, 5, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6, 5, 6, 6, 5, 6, 5, 5, 6, 5, 5, 5, 5, 5, 6, 1, 6, 6, 5, 5, 6, 1, 5, 6, 1, 5, 6, 6, 1, 5, 6, 6, 1, 5, 6, 1, 5, 6, 6, 1, 5, 6, 1, 5, 6, 1, 5, 6, 1, 6, 6, 1, 2, 6, 6, 1, 5, 6, 1, 5, 5, The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report