标签:span not nbsp val ack solution rac type div
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1 / 2 5 / \ 3 4 6
The flattened tree should look like:
1 2 3 4 5 6
1 # Definition for a binary tree node. 2 # class TreeNode(object): 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution(object): 9 def flatten(self, root): 10 """ 11 :type root: TreeNode 12 :rtype: None Do not return anything, modify root in-place instead. 13 """ 14 self.prev = None 15 def dfs(root): 16 if root is None: 17 return None 18 # reverse preOrder traveral 19 # use pre_node to track the previous node to connect as current right 20 dfs(root.right) 21 dfs(root.left) 22 root.right = self.prev 23 root.left = None 24 self.prev = root 25 dfs(root)
[LC] 114. Flatten Binary Tree to Linked List
标签:span not nbsp val ack solution rac type div
原文地址:https://www.cnblogs.com/xuanlu/p/11725036.html