114 Flatten Binary Tree to Linked List [Python]
Posted wyz-2020
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了114 Flatten Binary Tree to Linked List [Python]相关的知识,希望对你有一定的参考价值。
114 Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
将二叉树展开成链表
[[]D:dataStructureLeetcode114.png]
(D:dataStructureLeetcode114.png "114")
思路:将根节点与左子树相连,再与右子树相连。递归地在每个节点的左右孩子节点上,分别进行这样的操作。
代码
class Solution(object):
def flatten(self, root):
if root == None:
return
if root.left == None and root.right == None:
return
self.flatten(root.left)
self.flatten(root.right)
tmp = root.left
root.right = root.left
root.left = None
while root.right:
root = root.right
root.right = tmp
以上是关于114 Flatten Binary Tree to Linked List [Python]的主要内容,如果未能解决你的问题,请参考以下文章
114 Flatten Binary Tree to Linked List [Python]
114. Flatten Binary Tree to Linked List
114. Flatten Binary Tree to Linked List
114. Flatten Binary Tree to Linked List