剑指OFFER-二叉搜索树与双向链表

剑指OFFER-二叉搜索树与双向链表

Question

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
关键词:链表 二叉搜索树 中序遍历

Solution

中序遍历

由于搜索树节点大小为左<根<右,因此用中序遍历(左根右)二叉树得到排序号的链表,更新链表的left、right。
时间复杂度:O(N)
空间复杂度:O(N)

  • Python
class Solution:
    def Convert(self, pRootOfTree):
        if not pRootOfTree:
            return None
        lian = []
        
        def getList(node):
            if not node:
                return
            getList(node.left)
            lian.append(node)
            getList(node.right)
        
        getList(pRootOfTree)
        
        for i in range(len(lian)):
            if i > 0:
                lian[i].left = lian[i-1]
            if i < len(lian)-1:
                lian[i].right = lian[i+1]
        return lian[0]
  • C++
class Solution {
    vector<TreeNode*> lian;
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(!pRootOfTree)
            return NULL;
        getList(pRootOfTree);
        for(int i=0; i<lian.size(); i++){
            if(i > 0)
                lian[i]->left = lian[i-1];
            if(i < lian.size()-1)
                lian[i]->right = lian[i+1];
        }
        return lian[0];
    }
    
    void getList(TreeNode* p){
        if(!p)
            return;
        getList(p->left);
        lian.push_back(p);
        getList(p->right);
    }
};

递归

将问题拆分成:将左子树拆成有序链表(递归),右子树拆成有序链表(递归),当前根节点left指向左子树,right指向右子树。
时间复杂度:O(N)
空间复杂度:O(1)

  • Python
class Solution:
	# 返回二叉树转链表的表头
    def Convert(self, pRootOfTree):
        if not pRootOfTree:
            return None
        # 得到左子树转链表的表头
        left_h = self.Convert(pRootOfTree.left)
        # 得到右子树转链表的表头
        right_h = self.Convert(pRootOfTree.right)
        
        temp = left_h
        if temp:
            while temp.right:
                temp = temp.right
            # 左子树链表的表尾才是当前节点的左边
            temp.right = pRootOfTree
        pRootOfTree.left = temp
        
        # 右子树链表的表头就是当前节点的右边
        pRootOfTree.right = right_h
        if right_h:
            right_h.left = pRootOfTree
        #注意如果左子树不存在,则表头为当前节点,若当前节点不存在,在函数第一个判断中已经返回None
        return left_h if left_h else pRootOfTree 
  • C++
class Solution {
public:
    TreeNode* Convert(TreeNode* pRootOfTree)
    {
        if(!pRootOfTree)
            return NULL;
        TreeNode* left_h = Convert(pRootOfTree->left);
        TreeNode* right_h = Convert(pRootOfTree->right);
        
        TreeNode* temp = left_h;
        if(temp){
            while(temp->right)
                temp = temp->right;
            temp->right = pRootOfTree;
            pRootOfTree->left = temp;
        }
        
        pRootOfTree->right = right_h;
        if(right_h)
            right_h->left = pRootOfTree;
        
        return left_h? left_h:pRootOfTree;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值