-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path897.cpp
More file actions
29 lines (29 loc) · 747 Bytes
/
Copy path897.cpp
File metadata and controls
29 lines (29 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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 { // Runtime: 36 ms, faster than 94.74% of C++ online submissions
// for Increasing Order Search Tree.
public:
TreeNode *increasingBST(TreeNode *root) {
TreeNode *head = nullptr, *ret = nullptr;
function<void(TreeNode *)> inorder = [&](auto node) {
if (not node)
return;
inorder(node->left);
if (not head)
ret = head = node;
else
head = head->right = node;
head->left = nullptr;
inorder(node->right);
};
inorder(root);
return ret;
}
};