Leetcode sword finger offer interview question 36. Binary search tree and two-way linked list

Time:2021-9-9

Interview question 36. Binary search tree and two-way linked list

solution

Step 1: you need to point the left pointer to the previous value smaller than the element, and the right pointer to the next value larger than the current element. However, this value is not necessarily the father or son of the node. Therefore, during recursion, you need to pass the known boundary of the current element to your son.

And the right boundary passed to the left child is himself, and the left boundary is the boundary passed down by the father of the current element. For the right child, the opposite is true.

The left and right bounds of the starting root node of recursion are set to none

Step 2: connect the minimum and maximum values

How to find minimum and maximum, minimum and maximumnot alwaysIs a leaf node. I just started making this mistake.

The minimum value must be the left son of a node, and he has no left son. Is the leftmost son of the whole tree.

Therefore, the condition for finding the minimum node i define is: there is no left son + the left boundary passed by the father is none
Maximum node: no right son + the right boundary from the father is none

code

class Solution:
    def treeToDoublyList(self, root: 'Node') -> 'Node':
        if not root: return root
        def build(t, l, r):
            xl = t if not l else l
            xr = t if not r else r
            xxl, xxr = t, t
            if not t.left:
                if not l:
                    self.head = t
                else:
                    t.left = l
            else:
                ll, lr = build(t.left, l, t)
                t.left = lr
                xxl = ll
            if not t.right:
                if not r:
                    self.rear = t
                else:
                    t.right = r
            else:
                rl, rr = build(t.right, t, r)
                t.right = rl
                xxr = rr
            return xxl, xxr
        build(root, None, None)
        self.head.left = self.rear
        self.rear.right = self.head
        return self.head

Wrong writing

Here, it is erroneously assumed that the maximum and minimum values appear in the leaf node
Counterexample:

    3
   /
  1
   \
    2
class Solution:
    def treeToDoublyList(self, root: 'Node') -> 'Node':
        if not root: return root
        self.head = root
        self.rear = root
        def build(t, l, r):
            # print(t.val, t.left, t.right, l, r)
            if not t.left and not t.right:
                if not l:
                    self.head = t
                else:
                    t.left = l
                if not r:
                    self.rear = t
                else:
                    t.right = r
                return t, t
            xl = t if not l else l
            xr = t if not r else r
            xxl, xxr = t, t
            if not t.left:
                t.left = l
            else:
                ll, lr = build(t.left, l, t)
                t.left = lr
                xxl = ll
            if not t.right:
                t.right = r
            else:
                rl, rr = build(t.right, t, r)
                t.right = rl
                xxr = rr
            return xxl, xxr
        build(root, None, None)
        self.head.left = self.rear
        self.rear.right = self.head
        return self.head