题目描述
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
中序遍历二叉树,然后用一个vector保存遍历的结果,这样在vector中节点就按顺序保存了,然后再来修改指针,虽然没有一点技术含量,但是最后竟然还通过了
class Solution {
public:void get_list(TreeNode* pRootOfTree, vector<TreeNode*>& list){if(pRootOfTree->left != NULL)get_list(pRootOfTree->left, list);list.push_back(pRootOfTree);if(pRootOfTree->right != NULL)get_list(pRootOfTree->right, list);}TreeNode* get_ret(vector<TreeNode*>& list){for(int i=0; i<list.size()-1; i++){list[i]->right = list[i+1];list[i+1]->left = list[i];}return list[0];}TreeNode* Convert(TreeNode* pRootOfTree){if(pRootOfTree == NULL)return NULL;vector<TreeNode*> list;get_list(pRootOfTree, list);return get_ret(list);}
};