Algorithm

114. Flatten Binary Tree to Linked List

Doljae 2021. 5. 13. 15:35
 

Flatten Binary Tree to Linked List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

1. Post - Order

class Solution:
    def flatten(self, root: TreeNode) -> None:
        stack, temp = [], []
        cur = root
        while True:
            while cur:
                temp.append(cur)
                stack.append(cur)
                cur = cur.left
            if not stack:
                break
            cur = stack.pop()
            cur = cur.right
        for i in range(1, len(temp)):
            temp[i - 1].left = None
            temp[i - 1].right = temp[i]
        return temp[0] if temp else []

Post - Order 순회로 접근이 가능하다.