Created
March 29, 2022 17:24
-
-
Save 0xPratik/b33b5786be54f59d05181783a8e98d4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
class Node { | |
public: | |
int data; | |
Node *left; | |
Node *right; | |
Node(int d) { | |
data = d; | |
left = NULL; | |
right = NULL; | |
} | |
}; | |
class Solution { | |
public: | |
Node* insert(Node* root, int data) { | |
if(root == NULL) { | |
return new Node(data); | |
} else { | |
Node* cur; | |
if(data <= root->data) { | |
cur = insert(root->left, data); | |
root->left = cur; | |
} else { | |
cur = insert(root->right, data); | |
root->right = cur; | |
} | |
return root; | |
} | |
} | |
/* | |
class Node { | |
public: | |
int data; | |
Node *left; | |
Node *right; | |
Node(int d) { | |
data = d; | |
left = NULL; | |
right = NULL; | |
} | |
}; | |
*/ | |
void levelOrder(Node * root) { | |
queue<Node*> q; | |
q.push(root); | |
while (!q.empty()) | |
{ | |
Node* root = q.front(); | |
q.pop(); | |
cout << root->data << ' '; | |
if (root->left) q.push(root->left); | |
if (root->right) q.push(root->right); | |
} | |
} | |
}; //End of Solution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment