In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to construct the python binary tree from the preorder and the preorder traversal sequence, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
[title]
A binary tree is constructed according to the preorder traversal and intermediate order traversal of a tree.
Note: you can assume that there are no duplicate elements in the tree.
For example, give the
Preorder traversal preorder = [3pm 9pm 20je 15rem 7]
Traversing in the middle order inorder = [9 ~ 1 ~ 3 ~ 15 ~ 20 ~ 7]
Return the following binary tree:
three
/\
9 20
/\
15 7
[ideas]
First of all, review the order of traversal: the preorder traversal is the root node-left subtree-right subtree, and the middle order traversal is the left subtree-root node-right subtree.
Then the first element of the preorder traversal array must be the root node. If you find this element in the preorder traversal array, the first part is the element of the left subtree and the second part is the element of the right subtree. It can be solved by recursion.
Note: pre-order traversal + post-order traversal, can not determine the only binary tree!
[code]
Python version
# Definition for a binary tree node.
# class TreeNode (object):
# def _ _ init__ (self, x):
# self.val = x
# self.left = None
# self.right = None
Class Solution (object):
Def buildTree (self, preorder, inorder):
"
: type preorder: List [int]
: type inorder: List [int]
: rtype: TreeNode
"
# preorder traversal, the first one is head
# traversal in the middle order, the first part is the left subtree and the second part is the right subtree
If len (preorder) = = 0:
Return None
Node = TreeNode (preorder [0])
Index = inorder.index (preorder [0])
Node.left = self.buildTree (preorder [1: index + 1], inorder [: index])
Node.right = self.buildTree (preindex [index + 1:], inorder [index + 1:])
Return node
[similar topic]
Construction of binary Tree from Middle order and Post order ergodic sequences
The idea of solving the problem: the last element of the post-order traversal array is the element of the root node, which is also found in the order traversal array and recursively generates a binary tree.
Construction of binary tree based on preorder and postorder traversal
The idea of solving the problem: directly generate a binary tree with only the right child to meet the conditions.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.