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
  • 相关阅读:
    Extension Methods(扩展方法)
    linux面试题
    渗透测试 day4
    渗透测试 day3
    渗透测试 day2
    渗透测试 day1
    9.3 网络安全介绍
    9.2 Iptables
    8.30 进程管理
    8.29 linux的网络
  • 原文地址:https://www.cnblogs.com/chruny/p/5258770.html
Copyright © 2011-2022 走看看