/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//深入到左叶子
static void goAlongVine ( TreeNode* x, stack<TreeNode*>& s ) {
while ( x ) { s.push ( x ); x = x->left; }
}
//其实就是中序遍历
TreeNode* convertBiNode ( TreeNode* x) {
if (!x) return x;
TreeNode* first;
bool init(true);
TreeNode*pre;
stack<TreeNode*> s;
while ( true ) {
goAlongVine ( x, s );
if ( s.empty() ) break;
x = s.top();
s.pop();
//初始化头结点
if(init){first=x;pre=x;init=false;}
else{
//调整指向,x弹出就废了,可以任意把玩
pre->right=x;
x->left=0;
pre=pre->right;
}
x = x->right;
}
return first;
}
};