高级农民
- 积分
- 1465
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-9-5
- 最后登录
- 1970-1-1
|
C++代码~- // In-order Traverse, Iterative
- class Solution {
- public:
- TreeNode *bstToDoublyList(TreeNode *root) {
- if (!root) {
- return NULL;
- }
- TreeNode *head = NULL, *tail = NULL, *cur = root;
- stack<TreeNode *> s;
- while (cur || !s.empty()) {
- while (cur) {
- s.push(cur);
- cur = cur->left;
- }
- cur = s.top();
- s.pop();
- if (tail) {
- tail->right = cur;
- cur->left = tail;
- }
- // if tail == NULL, this happens when we are handling the first node~
- else {
- head = cur;
- }
- tail = cur;
- cur = cur->right;
- }
- // circular 要加
- tail->right = head;
- head->left = tail;
- return head;
- }
- };
复制代码 |
|