版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88737102
897. 递增顺序查找树
分析
- 中序遍历
- 二叉树的构造
/**
* 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:
TreeNode* increasingBST(TreeNode* root) {
if(root==NULL){
return root;
}
p=new TreeNode(-1);
TreeNode* head=p;
_increasingBST(root);
return head->right;
}
private:
void _increasingBST(TreeNode* root){
if(root->left!=NULL)
{
_increasingBST(root->left);
}
p->left=NULL;
TreeNode* t=new TreeNode(root->val);
p->right=t;
p=t;
if(root->right!=NULL)
{
_increasingBST(root->right);
}
return;
}
private:
TreeNode* p;
};