File tree Expand file tree Collapse file tree 1 file changed +62
-26
lines changed
Expand file tree Collapse file tree 1 file changed +62
-26
lines changed Original file line number Diff line number Diff line change 1+ /* *********************************************************
2+
3+ Following are the Binary Tree Node class structure and
4+ the Node class structure
5+
6+ template <typename T>
7+ class BinaryTreeNode {
8+ public:
9+ T data;
10+ BinaryTreeNode<T> *left;
11+ BinaryTreeNode<T> *right;
12+
13+ BinaryTreeNode(T data) {
14+ this->data = data;
15+ left = NULL;
16+ right = NULL;
17+ }
18+ };
19+
20+ template <typename T>
21+ class Node {
22+ public:
23+ T data;
24+ Node<T> *next;
25+ Node(T data) {
26+ this->data=data;
27+ this->next=NULL;
28+ }
29+ };
30+
31+ ***********************************************************/
132vector<Node<int >*> constructLinkedListForEachLevel (BinaryTreeNode<int > *root) {
2- vector<Node<int >*> vt;
33+
34+ vector<Node<int >*> vt;
335 if (root==NULL ){
436 vt.push_back (NULL );
537 return vt;
@@ -12,31 +44,35 @@ vector<Node<int>*> constructLinkedListForEachLevel(BinaryTreeNode<int> *root) {
1244 while (qu.size ()!=0 ){
1345 BinaryTreeNode<int >* temp = qu.front ();
1446 qu.pop ();
15- if (temp!=NULL ){
16- Node<int >* tempu = new Node<int >(temp->data );
17- if (start==NULL ){
18- start=tempu;
19- vt.push_back (start);
20- end=tempu;
21- }else {
22- end->next =tempu;
23- end=end->next ;
24- }
25- if (temp->left !=NULL ){
26- qu.push (temp->left );
27- }
28- if (temp->right !=NULL ){
29- qu.push (temp->right );
30- }
31- }else {
32- end->next =NULL ;
33- if (qu.size ()==0 ){
34- break ;
35- }
36- qu.push (NULL );
37- start=NULL ;
38- end=NULL ;
39- }
47+
48+ if (temp!=NULL ){
49+ Node<int >* amit = new Node<int >(temp->data );
50+ if (start==NULL ){
51+ start=amit;
52+ end=amit;
53+ }else {
54+ end->next =amit;
55+ end=end->next ;
56+ }
57+
58+ if (temp->left ){
59+ qu.push (temp->left );
60+ }
61+ if (temp->right ){
62+ qu.push (temp->right );
63+ }
64+
65+ }else {
66+ vt.push_back (start);
67+ if (qu.size ()==0 ){
68+ return vt;
69+ }
70+ start=NULL ;
71+ end=NULL ;
72+ qu.push (NULL );
73+ }
4074 }
4175 return vt;
76+
77+
4278}
You can’t perform that action at this time.
0 commit comments