zoukankan      html  css  js  c++  java
  • [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
            
    View Code
  • 相关阅读:
    HDU 6034
    HDU 6047
    CodeForces 830B
    HDU 4972
    HDU 4408
    CodeForces 788B
    CodeForces 788A
    CodeForces 792C
    uva 1658 Admiral 最小费最大流
    hdu 5391 Zball in Tina Town 威尔逊定理
  • 原文地址:https://www.cnblogs.com/chruny/p/5258770.html
Copyright © 2011-2022 走看看