欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

114. 二叉树展开为链表

程序员文章站 2024-03-26 13:59:11
...

114. 二叉树展开为链表 

114. 二叉树展开为链表

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def flatten(self, root):
        """
        :type root: TreeNode
        :rtype: None Do not return anything, modify root in-place instead.
        """
        while root:
            if root.left:
                most_node = root.left
                while most_node.right:
                    most_node = most_node.right
                most_node.right = root.right
                root.right = root.left
                root.left = None
            root = root.right

 

相关标签: 笔试题 LeetCode