-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.cpp
39 lines (36 loc) · 830 Bytes
/
flatten.cpp
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
32
33
34
35
36
37
38
39
#include "flatten.h"
#include <vector>
#include <iostream>
using namespace std;
TreeNode* tree2link(TreeNode* root) {
if(!root)
return root;
TreeNode* leftTree = tree2link(root->left);
TreeNode* rightTree = tree2link(root->right);
root->left = nullptr;
if(leftTree == nullptr && rightTree == nullptr){
cout<<"Entering Mode 1"<<endl;
return root;
}
if(leftTree == nullptr) {
cout<<"Entering Mode 2"<<endl;
root->right = rightTree;
return root;
}
if(rightTree == nullptr) {
cout<<"Entering Mode 3"<<endl;
root->right = leftTree;
return root;
}
cout<<"Entering Mode 4"<<endl;
root->right = leftTree;
TreeNode* tmp = leftTree;
while(tmp->right) {
tmp = tmp->right;
}
tmp->right = rightTree;
return root;
}
void Solution::flatten(TreeNode* root) {
root = tree2link(root);
}