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

二叉搜索树与双向链表

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:
	vector<TreeNode*> Tree;
	void InorderTraversal(TreeNode* root)
	{
		if(root==nullptr)
			return;
		InorderTraversal(root->left);
		Tree.push_back(root);
		InorderTraversal(root->right);
	}
    TreeNode* Convert(TreeNode* pRootOfTree) {
        if(pRootOfTree==nullptr)
			return pRootOfTree;
		InorderTraversal(pRootOfTree);
		for(int i=0;i<Tree.size()-1;i++)
		{
			Tree[i]->right=Tree[i+1];
			Tree[i+1]->left=Tree[i];
		}
		return Tree[0];
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务