NOWCODER 剑指offer 二叉搜索树与双向链表

原来题意的双向链表是left和right。小数指向大数是right,大数指向小数是left

看了一下非递归算法,很妙,刚好是中序遍历的非递归

运行时间:26ms

占用内存:5732k

需要注意第一次的时候,只是把中序遍历的第一个结点找到而已

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Convert(self, pRootOfTree):
        # write code here
        if not pRootOfTree:
            return None
        current = pRootOfTree
        nodelist = []
        firstnode = True
        while (current or nodelist):
            while (current):
                nodelist.append(current)
                current = current.left
            current = nodelist.pop()
            if (firstnode):
                root = current
                pre = root
                firstnode = False
            else:
                pre.right = current
                current.left = pre
                pre = current
            current = current.right
        return root

——————————————————————————————————————

插播一条递归与非递归的中序遍历

    #非递归   
    def MidSearch(self, pRootOfTree):
        # write code here
        if not pRootOfTree:
            return None
        current = pRootOfTree
        nodelist = []
        # firstnode = True
        result = []
        while (current or nodelist):
            while (current):
                nodelist.append(current)
                current = current.left
            current = nodelist.pop()
            result.append(current.val)
            current = current.right
        return result
 
    #递归
    def MidSearch(self, pRootOfTree):
        # write code here
        res = []
        if not pRootOfTree:
            return None
        if (pRootOfTree.left):
            res += self.MidSearch(pRootOfTree.left)
        res += [pRootOfTree.val]
        if (pRootOfTree.right):
            res += self.MidSearch(pRootOfTree.right)
        return res

——————————————————————————————————————

递归算法

运行时间:26ms

占用内存:5724k

考虑左右子树分开而且连接的过程

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def Convert(self, pRootOfTree):
        # write code here
        if not pRootOfTree:
            return None
        firstNode = pRootOfTree
        if (not pRootOfTree.left and not pRootOfTree.right):
            firstNode = pRootOfTree
            return pRootOfTree
        l = self.Convert(pRootOfTree.left)
        if (l):
            firstNode = l
            while (l.right):
                l = l.right
            l.right = pRootOfTree
            pRootOfTree.left = l
        r = self.Convert(pRootOfTree.right)
        if (r):
            pRootOfTree.right = r
            r.left = pRootOfTree
        return firstNode

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值