Skip to content

Instantly share code, notes, and snippets.

@0xPratik
Created March 29, 2022 17:24
Show Gist options
  • Save 0xPratik/b33b5786be54f59d05181783a8e98d4e to your computer and use it in GitHub Desktop.
Save 0xPratik/b33b5786be54f59d05181783a8e98d4e to your computer and use it in GitHub Desktop.
#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