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
  • 相关阅读:
    csuoj 漫漫上学路
    sql函数
    sql基本
    查看webdriver API
    Jmeter应用-接口测试
    http协议
    Jmeter .jmx 改为.jtl
    Jmeter遇到打不开的问题
    测试要点
    apt-get安装mysql
  • 原文地址:https://www.cnblogs.com/chruny/p/5258770.html
Copyright © 2011-2022 走看看