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

二叉搜索树与双向链表

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;
    }
};

全部评论

相关推荐

循此苦旅:月初笔试全a,没面试!
点赞 评论 收藏
分享
hso_:哈哈哈哈哈哈我没offer一样在同一道题开喷了
投递深圳同为数码等公司10个岗位
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务