-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLevelOrder
38 lines (33 loc) · 931 Bytes
/
getLevelOrder
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
#include <bits/stdc++.h>
/************************************************************
Following is the BinaryTreeNode class structure
template <typename T>
class BinaryTreeNode {
public:
T val;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;
BinaryTreeNode(T val) {
this->val = val;
left = NULL;
right = NULL;
}
};
************************************************************/
vector<int> getLevelOrder(BinaryTreeNode<int> *root)
{
queue<BinaryTreeNode<int>*> q;
q.push(root);
vector<int> ans;
if(root == NULL || root->val == -1){
return ans;
}
while(!q.empty()){
BinaryTreeNode<int>* temp = q.front();
ans.push_back(temp->val);
q.pop();
if(temp->left != NULL) q.push(temp->left);
if(temp->right != NULL) q.push(temp->right);
}
return ans;
}