题目:原题链接(中等)

标签:树

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) O ( N ) O(N) O(N) O ( N ) O(N) O(N) 44ms (60.00%)
Ans 2 (Python)
Ans 3 (Python)

解法一:

class Solution:
    def flipBinaryTree(self, root: 'Node', leaf: 'Node') -> 'Node':
        if root == leaf:
            return root

        leaf.left = leaf.parent
        leaf.parent = None

        node = leaf
        while node.left != root:
            if node.left.right == node:
                node.left.right = node.left.left

            node.left.left = node.left.parent
            node.left.parent = node

            node = node.left

        if node.left.left == node:
            node.left.left = None
        else:
            node.left.right = None

        node.left.parent = node
        
        return leaf