Wins the Offer (26): binary search trees and doubly-linked list

Wins the Offer (26): binary search trees and doubly-linked list

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners

First, the primer

This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click CSDN and github link:
prove safety Offer complete analytical exercises CSDN address
github address

Second, the title

Enter a binary search tree, the binary search tree converted into a doubly linked list sorted. Requirements can not create any new node, point to only adjust the tree node pointer.

1, ideas

First Give an example:

file

Binary search tree as shown above, we need to convert it with a doubly linked list.

According to the characteristics of the binary search tree: value <root node <node left and right values ​​of nodes, we find, using the ordinal number of the binary tree traversal sequence out data is sorted. So, first, to determine the method of traversing a binary search tree.

Among preorder traversal to the root, we put as binary search trees Part 3: value of 10 nodes, the root node 6 of the left subtree, the root node 14 of the right subtree. The sorted linked list is defined, the maximum value of a node junction point 10 and its left subtree is linked to node 8, and it will be the smallest and the right subtree of the node 12 is nodes linked. As shown below:

file

Note: root, left subtree, right subtree. After the left and right subtrees are converted into a doubly linked list sorted and then link the root, whole pieces of binary search tree will become a sort of conversion doubly linked list

According recursive traversal sequence, when we traverse conversion to the root node, its left subtree has been converted into a sorted list, and at this time the value of the list is the left child node of the tree the maximum value of the tail (8). We see it (8) and root (10) are linked, then the root node (10) becomes the tail of the list, and then to traverse the right subtree, we know that in order to traverse the root node (10) after a junction in this case, the minimum value of the right subtree of the node (12), and it will be linked root. Left and right subtrees then such a way that recursion can solve the problem.

2, programming

python

Code implementation:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Convert(self, pRootOfTree):
        # write code here
        if not pRootOfTree: return
        self.arr = []
        self.midTraversal(pRootOfTree)
        for i,v in enumerate(self.arr[:-1]):
            v.right = self.arr[i + 1]
            self.arr[i + 1].left = v
        return self.arr[0]
        
    ##中序遍历
    def midTraversal(self, root):
        if not root: return
        self.midTraversal(root.left)
        self.arr.append(root)
        self.midTraversal(root.right)

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11445308.html
Recommended