0897.cpp (535B)
1 class Solution { 2 public: 3 TreeNode *increasingBST(TreeNode *root) { 4 TreeNode *head, *tmp; 5 tmp = head = new TreeNode(); 6 stack<TreeNode *> st; 7 while (true) { 8 while (root) { 9 st.push(root); 10 root = root->left; 11 } 12 if (st.empty()) break; 13 root = st.top(); 14 st.pop(); 15 tmp = tmp->right = root; 16 tmp->left = nullptr; 17 root = root->right; 18 } 19 return head->right; 20 } 21 };