标签:str size 非递归 搜索 描述 利用 root treenode 16px
题目描述
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: TreeNode* Convert(TreeNode* pRootOfTree) { if(pRootOfTree == nullptr) return nullptr; // 非递归算法,故用堆栈 stack<TreeNode*> sck; TreeNode *pCur = pRootOfTree; TreeNode *pNode = nullptr; // 存储前一个输出结点 TreeNode *pHead = nullptr; // 存储有序链表表头 while(!sck.empty() || pCur != nullptr) { while(pCur != nullptr) { sck.push(pCur); pCur = pCur->left; } pCur = sck.top(); // 处理输出结点 if(pNode != nullptr) { pNode->right = sck.top(); sck.top()->left = pNode; pNode = sck.top(); } if(pNode == nullptr) { pNode = sck.top(); pHead = pNode; } pNode = sck.top(); sck.pop(); if(pCur->right) { pCur = pCur->right; } else { pCur = nullptr; } } return pHead; } };
标签:str size 非递归 搜索 描述 利用 root treenode 16px
原文地址:https://www.cnblogs.com/xzxl/p/9611161.html