赞
踩
题目来源: 114.二叉树展开为链表
题目: 给你二叉树的根结点 root ,请你将它展开为一个单链表:



# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: None Do not return anything, modify root in-place instead. """ if not root: return self.flatten(root.left) self.flatten(root.right) if root.left: temp = root.left while (temp.right): temp = temp.right temp.right = root.right root.right = root.left root.left = None return root

参考链接: 【面试高频题】将二叉树展开为链表
二叉树展开为链表(python)
【LeetCode 114】二叉树展开为链表(Python)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。