注意左子树是空的
/**
* 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 {
private:
TreeNode* init = NULL;
TreeNode* p1 = NULL,*p2 = NULL;
public:
TreeNode* increasingBST(TreeNode* root) {
print(root);
return init;
}
void print(TreeNode* p)
{
if(!p) return;
print(p->left);
p->left = NULL;
if(!init) init = p;
p1 = p2;
p2 = p;
if(p1)
{
cout<<p1->val<<" "<<p2->val<<endl;
p1->right = p2;
p1->left = NULL;
}
print(p->right);
}
};