-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
121 lines (109 loc) · 2.81 KB
/
Copy pathBinaryTree.cpp
File metadata and controls
121 lines (109 loc) · 2.81 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
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
// There are 5 types of BT
// 1. Full Binary Tree - every node has 0 or 2 children
// 2. Complete Binary Tree - all levels are completely filled except possibly the last level, which is filled from left to right
// 3. Perfect Binary Tree - all leaf nodes are at the same level
// 4. Balanced Binary Tree - the height of tree can be at most log(n)
// 5. Degenerate Binary Tree (or pathological tree)
// Traversal Techniques - breadth first search(BFS) and depth first search(DFS)
// DFS - Inorder, Preorder, Postorder
// Inorder - left, root, right
// Preorder - root, left, right
// Postorder - left, right, root
// BFS - go line by line / level order traversal - uses queue data structure
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left;
Node *right;
Node(int val)
{
data = val;
left = right = NULL;
}
};
void preOrder(Node *root)
{
// Time Complexity - O(n)
// Space Complexity - O(h) - h is the height of the tree, in case of skewed tree it can be O(n) - auxiliary space
if (root == NULL)
return;
cout << root->data << " ";
preOrder(root->left);
preOrder(root->right);
}
vector<int> preorderTraversal(Node *root)
{
// Time Complexity - O(n)
// Space Complexity - O(n) - auxiliary space
vector<int> ans;
if (root == NULL)
return ans;
stack<Node *> st;
st.push(root);
while (!st.empty())
{
Node *temp = st.top();
st.pop();
ans.emplace_back(temp->data);
if (temp->right)
st.push(temp->right);
if (temp->left)
st.push(temp->left);
}
return ans;
}
void inOrder(Node *root)
{
// Time Complexity - O(n)
// Space Complexity - O(n)
if (root == NULL)
return;
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
void postOrder(Node *root)
{
// Time Complexity - O(n)
// Space Complexity - O(n)
if (root == NULL)
return;
postOrder(root->left);
postOrder(root->right);
cout << root->data << " ";
}
vector<vector<int>> levelOrder(Node *root)
{
vector<vector<int>> ans;
if (root == NULL)
return ans;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
int s = q.size();
vector<int> level;
for (int i = 0; i < s; i++)
{
Node *node = q.front();
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
level.emplace_back(node->data);
}
ans.emplace_back(level);
}
return ans;
}
int main()
{
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->right = new Node(5);
preOrder(root); // Output: 1 2 5 3
}