class Solution {
public:
/**
* @param root: The root of tree
* @return: the head of doubly list node
*/
DoublyListNode* bstToDoublyList(TreeNode* root) {
// Write your code here
return getDouble(root);
}
DoublyListNode* getDouble(TreeNode* node){
DoublyListNode* root=NULL;
DoublyListNode* prenode = NULL;
DoublyListNode* curnode = NULL;
stack<TreeNode*> rightnodestack;
TreeNode* tmp = node;
while (tmp!=NULL){
rightnodestack.push(tmp);
tmp = tmp->left;
}
while (rightnodestack.empty()==false){
if (root == NULL){
root = new DoublyListNode(rightnodestack.top()->val);
root->prev = NULL;
prenode = root;
}
else{
curnode = new DoublyListNode(rightnodestack.top()->val);
curnode->prev = prenode;
prenode->next = curnode;
prenode = curnode;
}
//cout<<rightnodestack.top()->val<<endl;
TreeNode* tmp = rightnodestack.top()->right;
rightnodestack.pop();
while (tmp!=NULL){
rightnodestack.push(tmp);
tmp = tmp->left;
}
}
return root;
}
};
LintCode-剑指Offer-(378)将二叉查找树转换成双链表
原文作者:二叉查找树
原文地址: https://blog.csdn.net/u011464853/article/details/50096051
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/u011464853/article/details/50096051
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。