-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday3.cpp
More file actions
31 lines (30 loc) · 880 Bytes
/
day3.cpp
File metadata and controls
31 lines (30 loc) · 880 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
30
31
/**
* 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 {
public:
TreeNode* head = new TreeNode(0); //the topmost node actually is declared null
TreeNode* ptr = head; //the ptr traverses
void dfs(TreeNode* root)
{
if(root==NULL)
return;
dfs(root->left);
head->right= new TreeNode(root->val);
head=head->right;
dfs(root->right);
return;
}
TreeNode* increasingBST(TreeNode* root) {
dfs(root);
return ptr->right; //returning the right increasing order BST
}
};