题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
	void past_order(TreeNode* cur) {
		if (!cur || (!cur->left && !cur->right) ) return;
		past_order(cur->left);
		past_order(cur->right);
		TreeNode* temp1 = cur->left;
		if (temp1 != nullptr) {
			while (temp1->right) temp1 = temp1->right;
			cur->left = temp1, temp1->right = cur;
		}
		temp1 = cur->right;
		if (temp1 != nullptr) {
			while (temp1->left) temp1 = temp1->left;
			cur->right = temp1, temp1->left = cur;
		}
	}
    TreeNode* Convert(TreeNode* root) {
		if (!root) return root;
		TreeNode* res = root;
		while (res->left) res = res->left;
        past_order(root);
		return res;
    }
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:00
林子大了什么鸟都有啊,我觉得我说的已经很客气了,阴阳谁呢
牛客62656195...:应该不是阴阳吧?你第一次注册的时候boss就说你是牛人
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
07-09 15:14
南京大学 C++
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务