JZ36 二叉搜索树与双向链表C++

JZ36 二叉搜索树与双向链表C++,第1张

链接:

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&&tqId=11179&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

描述:



代码:
/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void InOrder(TreeNode* cur,TreeNode*& prev){
        if(cur == nullptr) return;
        
        InOrder(cur->left, prev);
        cur->left = prev;
        if(prev) prev->right=cur;
        prev=cur;
        
        InOrder(cur->right, prev);
    }
    
    TreeNode* Convert(TreeNode* pRootOfTree) {
        if(pRootOfTree==NULL) return NULL;
        
        TreeNode* prev= nullptr;
        InOrder(pRootOfTree, prev);
        TreeNode* head = pRootOfTree;
        while(head->left){
            head = head->left;
        }
        return head;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/920996.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存