Zero to Hero
article thumbnail
 

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 순회로 접근이 가능하다.

'Algorithm' 카테고리의 다른 글

394. Decode String  (0) 2021.05.14
621. Task Scheduler  (0) 2021.05.14
62. Unique Paths  (0) 2021.05.12
64. Minimum Path Sum  (0) 2021.05.12
199. Binary Tree Right Side View  (0) 2021.05.11
profile

Zero to Hero

@Doljae

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!