Convert

114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

def flatten(self, root: TreeNode) -> None:
    self.preNode = None
    
    def helper(node):
        nonlocal preNode
        if not node: return None
        helper(node.right)
        helper(node.left)
        
        node.right = preNode
        node.left = None
        preNode = node
        
    helper(root)
def flatten(self, root: TreeNode) -> None:
    if not root: return
    
    node = root
    
    while node:
        if node.left: 
            rightMost = node.left

            while rightMost.right:
                rightMost = rightMost.right
            
            rightMost.right = node.right
            node.right = node.left
            node.left = None
            
        node = node.right
        

106t Convert Sorted LinkedList to Balanced BST

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

  • Divide and Conquer

426 Convert BST to double linked list

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.並且首尾相連

Last updated

Was this helpful?