# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def flatten(self, root: TreeNode) -> None:
"""
Do not return anything, modify root in-place instead.
"""
# base case
if not root:
return
while root:
if not root.left:
root = root.right
else:
# find the rightmost node of the left subtree
node = root.left
print(type(node))
while node.right:
node = node.right
# joint
node.right = root.right
root.right = root.left
# update
root.left = None
root = root.right
刷题记录:二叉树展开为链表
最新推荐文章于 2024-07-04 10:15:20 发布