码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 114. Flatten Binary Tree to Linked List

时间:2019-10-23 12:06:42      阅读:102      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!