LeetCode 897. 递增顺序搜索树
原题链接
简单
作者:
回归线
,
2021-04-25 14:16:47
,
所有人可见
,
阅读 314
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
TreeNode* tmp;
public:
TreeNode* increasingBST(TreeNode* root) {
TreeNode* dummy = new TreeNode(-1);
tmp = dummy;
inorder(root);
return dummy->right;
}
void inorder(TreeNode* root) {
if (!root) {
return;
}
inorder(root->left);
tmp->right = root;
root->left = nullptr;
tmp = root;
inorder(root->right);
}
};