-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103-Binary-Tree-Zigzag-Level-Order-Traversal.cpp
More file actions
46 lines (40 loc) · 1.34 KB
/
103-Binary-Tree-Zigzag-Level-Order-Traversal.cpp
File metadata and controls
46 lines (40 loc) · 1.34 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
class Solution {
public:
int level(TreeNode* root){// it finds the no. of levels/height of the tree
if(root == NULL) return 0;
return 1 + max(level(root->left) , level(root->right));
}
void nLevel(TreeNode* root , vector<vector<int>> &ans){
// responsible for calling the fn so that values can be inserted in the Arrays
int n = level(root);
for(int i=1 ; i<=n ; i++){
vector<int> v;
if(i%2!=0) LtoR(root , 1 , i, v);
else RtoL(root , 1 , i, v);
ans.push_back(v);
}
}
void LtoR(TreeNode* root , int current , int level , vector<int> &v){
if(root==NULL) return;
if(current == level){
v.push_back(root->val);
return;
}
LtoR(root->left , current+1 , level , v);
LtoR(root->right , current+1 , level , v);
}
void RtoL(TreeNode* root , int current , int level , vector<int> &v){
if(root==NULL) return;
if(current == level){
v.push_back(root->val);
return;
}
RtoL(root->right , current+1 , level , v);
RtoL(root->left , current+1 , level , v);
}
vector<vector<int>> zigzagLevelOrder(TreeNode* root) { // main fun
vector<vector<int>> ans;
nLevel(root , ans);
return ans;
}
};