[LeetCode]题解(python):114-Flatten Binary Tree to Linked List
题目来源:
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
题意分析:
给出一个二叉树,将这个二叉树变成一个单链形式。
题目思路:
递归的思想,先将根节点连接左子树,然后再连接右子树。
代码(python):
# 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: void Do not return anything, modify root in-place instead. """ if root == None: return if root.left == None and root.right == None: return self.flatten(root.left);self.flatten(root.right) tmp = root.right root.right = root.left root.left = None while root.right: root = root.right root.right = tmp