-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeADV.cpp
More file actions
75 lines (70 loc) · 1.61 KB
/
Copy pathtreeADV.cpp
File metadata and controls
75 lines (70 loc) · 1.61 KB
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
#include <bits/stdc++.h>
using namespace std;
struct Node{
int key;
Node *left;
Node *right;
Node(int k){
key = k;
left = right = NULL;
}
};
void LevelOrderTraversal_linebyline(Node *root){ //O(n) time, O(n) space or theta(width) space
if(root == NULL){
return;
}
queue<Node*> q;
q.push(root);
q.push(NULL);
while(q.size() > 1){
Node* curr = q.front();
q.pop();
if(curr == NULL){
cout << "\n";
q.push(NULL);
continue;
}
cout << curr -> key << " ";
if(curr -> left != NULL){
q.push(curr -> left);
}
if(curr -> right != NULL){
q.push(curr -> right);
}
}
}
void PrintNodes_distK(Node* root, int k){ //Worst case: theta(n), Best case: theta(1), O(h) space
if(root == NULL){
return;
}
if(k == 0){
cout << root -> key << " ";
}
else{
PrintNodes_distK(root -> left, k - 1);
PrintNodes_distK(root -> right, k - 1);
}
}
void printLeftView(Node* root){
if(root == NULL){
return;
}
cout << root -> key << " ";
if(root -> left != NULL){
printLeftView(root -> left);
}
else{
printLeftView(root -> right);
}
}
int main(){
Node *root = new Node(10);
root -> left = new Node(20);
root -> right = new Node(30);
root -> right -> left = new Node(40);
root -> right -> right = new Node(50);
// LevelOrderTraversal_linebyline(root);
// PrintNodes_distK(root, 2);
printLeftView(root);
return 0;
}