Leetcode 114. Flatten Binary Tree to Linked List (Python edition)

Source: Internet
Author: User

Topic:

Given a binary tree, flatten it to a linked list in-place.

Algorithm ideas:

In fact, the problem is the transformation of binary tree pre-sequence traversal

My code inherits Leetcode 144. Binary Tree Preorder Traversal

Code:

Class solution (object):     def preordertraversal (self, root):          "" "        :type root:  treenode        :rtype: list[int]          "" "        if root == none:return  []        stack = [root]         result = []        while len (Stack)  != 0:            tmp_root =  Stack.pop ()             if tmp_root ==  none:continue            result.append (Tmp_ Root)    &nbSp;        stack.append (tmp_root.right)              stack.append (Tmp_root.left)          return result    def flatten (self, root):          "" "        :type root:  treenode        :rtype: void do not return  anything, modify root in-place instead.         "" "         result = self.preordertraversal (Root)          for i in range (1,len (result)):             result[i-1].left = None             result[i-1].right = result[i] 


Leetcode 114. Flatten Binary Tree to Linked List (Python edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.