题目:
题目描述:
给定一个二叉树,原地将它展开为链表。
例如,给定二叉树
将其展开为:
代码如下:
class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: None Do not return anything, modify root in-place instead. """ #思路:由示例图可知:对于每一颗子树进行后序遍历,将右子树连接到左子树的右子树上,将左子树连接到根节点的右子树上;递归进行遍历 if root == None: return None self.flatten(root.left) self.flatten(root.right) if root.left != None: p = root.left while p.right != None: p = p.right p.right = root.right root.right = root.left root.left = None