[LC] 114. Flatten Binary Tree to Linked List

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LC] 114. Flatten Binary Tree to Linked List相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

LeetCode 114. Flatten Binary Tree to Linked List

114. Flatten Binary Tree to Linked List

!!!!!!114. Flatten Binary Tree to Linked List