-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree_104_Maximum_Height.cpp
executable file
·140 lines (130 loc) · 3.5 KB
/
Tree_104_Maximum_Height.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *parent;
TreeNode *left;
TreeNode *right;
TreeNode(int x, TreeNode* parent) : val(x), parent(parent), left(NULL), right(NULL) {}
};
#include <stack>
template <typename Datatype>
class Queue {
public:
empty
private:
int size;
}
class Solution {
public:
int maxDepth_Recursion(TreeNode* root) {
//performance rate: 50.46%
if (!root) return 0;
else if (!root->left && !root->right) return 1;
else if (!root->left) return maxDepth(root->right)+1;
else if (!root->right) return maxDepth(root->left)+1;
else {
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return left >= right ? left+1 : right+1;
}
}
TreeNode* travleAlongLeft(TreeNode* root, int& level){
TreeNode* current = root;
while(current->left) {
current = current->left;
level++;
}
return current;
}
TreeNode* climbBack(TreeNode* current, int& level){
//root should not have no children
while(current->parent && current->parent->right==current) {
current = current->parent;
level--;
}
level--;
return current->parent;
}
int maxDepth_WithParent(TreeNode* current) {
if (!current) return 0;
int backtrack = 0;
int max_level = 1;
int current_level = 1;
while(current){
if (backtrack == 0){
current = travleAlongLeft(current, current_level);
}
if (current->right) {
current = current->right;
current_level++;
backtrack = 0;
}
else {
backtrack = 1;
max_level = (max_level >= current_level ? max_level : current_level);
current = climbBack(current, current_level);
}
}
return max_level;
}
int maxDepth(TreeNode* root){
//50.46
if(!root) return 0;
queue<TreeNode*> que;
que.push(root);
int max_level = 1;
int count_on_this_level = 1;
int count_on_next_level = 0;
while(!que.empty()){
if(count_on_this_level==0){
max_level++;
count_on_this_level=count_on_next_level;
count_on_next_level=0;
}
TreeNode* current = que.front();
que.pop();
count_on_this_level--;
if(current->left) {
que.push(current->left);
count_on_next_level++;
}
if(current->right) {
que.push(current->right);
count_on_next_level++;
}
}
return max_level;
}
};
int main(){
TreeNode* EmptyTree = NULL;
TreeNode* OneNodeTree = new TreeNode(1, NULL);
TreeNode* LeftTree = new TreeNode(1, NULL);
LeftTree->left = new TreeNode(2, LeftTree);
LeftTree->left->left = new TreeNode(3, LeftTree->left);
TreeNode* BigTree = new TreeNode(1, NULL);
BigTree->left = new TreeNode(2, BigTree);
BigTree->left->left = new TreeNode(3, BigTree->left);
BigTree->left->right = new TreeNode(4, BigTree->left);
BigTree->right = new TreeNode(5, BigTree);
BigTree->right->right = new TreeNode(6, BigTree->right);
// 1
// 2 5
// 3 4 6
Solution Sol = Solution();
cout<<"Emptytree: "<<Sol.maxDepth(EmptyTree)<<endl;
cout<<"OneNodeTree: " <<Sol.maxDepth(OneNodeTree)<<endl;
cout<<"LeftTree: " <<Sol.maxDepth(LeftTree)<<endl;
cout<<"BigTree: " <<Sol.maxDepth(BigTree)<<endl;
//cout<<Sol.traverse(BigTree);
}