0% found this document useful (0 votes)
16 views

Searchinbst Binarytreenode Root K Root Null: Bool If Return False

Uploaded by

yugank942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Searchinbst Binarytreenode Root K Root Null: Bool If Return False

Uploaded by

yugank942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

/**********************************************************

Following is the Binary Tree Node class structure

template <typename T>


class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;

BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};

***********************************************************/

bool searchInBST(BinaryTreeNode<int> *root , int k) {


// Write your code here
if(root == NULL){
return false;
}

if(root->data == k){
return true;
}

if(root->data > k){


//element is not present at right side
bool ans1 = searchInBST(root->left, k);
return ans1;
// if(ans1){
// return true;
// }else{
// return false;
// }

if (root->data < k) {
// element is present at the right side
bool ans2 = searchInBST(root->right, k);
return ans2;
}
}
/**********************************************************

Following is the Binary Tree Node class structure

template <typename T>


class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;

BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};

***********************************************************/

void elementsInRangeK1K2(BinaryTreeNode<int>* root, int k1, int k2) {


// Write your code here
if(root == NULL){
return;
}

if(root->data >= k1 && root->data <= k2){


elementsInRangeK1K2(root->left, k1, k2);
cout<<root->data<<" ";
elementsInRangeK1K2(root->right, k1, k2);
}
else if(k1 > root->data){
// if min of range is greater than root->data
elementsInRangeK1K2(root->right, k1, k2);

}
else if(k2 < root->data){
// if max of range is smaller than the root->data
elementsInRangeK1K2(root->left, k1, k2);
}
}
/**********************************************************
Following is the Binary Tree Node class structure

template <typename T>


class BinaryTreeNode {
public :
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;

BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};

***********************************************************/

#include<climits>

bool isBST2(BinaryTreeNode<int> *root, int min = INT_MIN , int max = INT_MAX) {


if (root == NULL) {
return true;
}

if (root->data < min || root->data > max) {


return false;
}

bool isleftOK = isBST2(root->left , min , root->data - 1);


bool isrightOK = isBST2(root->right , root->data , max);

return isleftOK && isrightOK;


}

bool isBST(BinaryTreeNode<int> *root) {


// Write your code here
if(root == NULL){
return true;
}

return isBST2(root , INT_MIN , INT_MAX);

BinaryTreeNode<int>* constructTreeHelper(int* arr , int si , int ei){

if(ei < si){


return NULL;
}

int midIndex = si + (ei-si) / 2; //Dry Run !!

// Make this middle element as root element


BinaryTreeNode<int>* root = new BinaryTreeNode<int>(arr[midIndex]);

//call for making the left subtree


BinaryTreeNode<int>* leftTree = constructTreeHelper(arr , si , midIndex-1);
//call for making the right subtree
BinaryTreeNode<int>* rightTree = constructTreeHelper(arr , midIndex+1 , ei);

//Now attach the left and the right subtree with the root node
root->left = leftTree;
root->right = rightTree;

return root;

BinaryTreeNode<int>* constructTree(int *input, int n) {


// Write your code here
return constructTreeHelper(input , 0 , n-1);
}
class Pair{
public:
Node<int>* head;
Node<int>* tail;
Pair(){
head = NULL;
tail = NULL;
}
};

Pair constructLinkedListHelper(BinaryTreeNode<int>* root){


if(root == NULL){
Pair output;
output.head = NULL;
output.tail = NULL;
return output;
}

Node<int>* rootnode = new Node<int>(root->data);

// ask the leftsubtree for its linked list


Pair leftLL = constructLinkedListHelper(root->left);
//ask right subtree for its linked list
Pair rightLL = constructLinkedListHelper(root->right);

Pair output;

if(leftLL.head == NULL && rightLL.head == NULL){


//only root is present
output.head = rootnode;
output.tail = rootnode;
}else if(leftLL.head == NULL && rightLL.head != NULL){

//only rightsubtree is present


output.head = rootnode;
rootnode->next = rightLL.head;
output.tail = rightLL.tail;
}else if(leftLL.head != NULL && rightLL.head == NULL ){

//only left subtree is present


output.head = leftLL.head;
leftLL.tail->next = rootnode;
output.tail = rootnode;
}else{

//both left and right subtrees are present


output.head = leftLL.head;
leftLL.tail->next = rootnode;
rootnode->next = rightLL.head;
output.tail = rightLL.tail;
}

return output;

Node<int>* constructLinkedList(BinaryTreeNode<int>* root) {


// Write your code here
return constructLinkedListHelper(root).head;
}
/**********************************************************

Following is the Binary Tree Node class structure

template <typename T>


class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;

BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};

***********************************************************/
#include <vector>
vector<int>* getPath(BinaryTreeNode<int> *root , int data) {
// Write your code here
if(root == NULL){
return NULL;
}

if(root->data == data){
vector<int>* output = new vector<int>();
output->push_back(root->data);
return output;
};

if(root->data > data){

//only left tree can have that data


vector<int>* leftPath = getPath(root->left, data);
if(leftPath != NULL){
leftPath->push_back(root->data);
}
return leftPath;
}

if(root->data <data){
//only right tree can have data
vector<int>* rightPath = getPath(root->right, data);
if(rightPath != NULL){
rightPath->push_back(root->data);
}
return rightPath;
}

}
/**********************************************************
Following is the Binary Tree Node class structure

template <typename T>


class BinaryTreeNode {
public:
T data;
BinaryTreeNode<T> *left;
BinaryTreeNode<T> *right;

BinaryTreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};

***********************************************************/

class BST {
// Define the data members
BinaryTreeNode<int>* root;
public:
BST() {
// Implement the Constructor
root == NULL;
}

BinaryTreeNode<int>* getMax(BinaryTreeNode<int>* root){

/*----------------- Public Functions of BST -----------------*/

BinaryTreeNode<int>* removeHelper(int data , BinaryTreeNode<int>* root){


// If tree is empty
if(root == NULL){
return NULL;
}

if(root->data > data){


//data can be only in left tree
BinaryTreeNode<int>* left_output = removeHelper(data, root->left);
return left_output;
}
if(root->data < data){
//data can only be in right tree
BinaryTreeNode<int>* right_output = removeHelper(data, root->right);
return right_output;
}
if(root->data == data){
if(root->left == NULL && root->right == NULL){
return NULL;
}else if(root->left == NULL && root->right != NULL){
return root->right;
}else if(root->left != NULL && root->right == NULL){
return root->left;
}else if(root->left != NULL && root->right != NULL){

}
}

void remove(int data) {


// Implement the remove() function

void print() {
// Implement the print() function
}

void insert(int data) {


// Implement the insert() function
}

bool search(int data) {


// Implement the search() function
}
};

=====================================================================================
void insertDuplicateNode(BinaryTreeNode<int> *root) {

// Write your code here


if (root == NULL)
return;
if (root->left != NULL) {
BinaryTreeNode<int> *temp1 = root->left;
BinaryTreeNode<int> *temp2 = new BinaryTreeNode<int>(root->data);
root->left = temp2;
temp2->left = temp1;
} else {
BinaryTreeNode<int> *temp = new BinaryTreeNode<int>(root->data);
root->left = temp;
}
insertDuplicateNode(root->left->left);
insertDuplicateNode(root->right);
}
#include <bits/stdc++.h>
int k = 0;
void convert(BinaryTreeNode<int> *root, int *arr) {
if (root == NULL)
return;
arr[k++] = root->data;
convert(root->left, arr);
convert(root->right, arr);
}

void pairSum(BinaryTreeNode<int> *root, int sum) {


int arr[100000000];

convert(root, arr);

sort(arr, arr + k);

int i = 0;
int j = k - 1;

while (i < j) {
if (arr[i] + arr[j] == sum) {
cout << arr[i] << " " << arr[j] << endl;
i++;
j--;
} else if (arr[i] + arr[j] > sum) {
j--;
} else if (arr[i] + arr[j] < sum)
i++;
}
}

int getLCA(BinaryTreeNode<int> *root, int a, int b) {


// Write your code here
if (root == NULL)
return -1;
if (root->data == a || root->data == b)
return root->data;

int left = getLCA(root->left, a, b);


int right = getLCA(root->right, a, b);

if (left == -1 && right == -1)


return -1;
else if (left != -1 && right == -1)
return left;
else if (left == -1 && right != -1)
return right;
else
return root->data;
}

int getLCA(BinaryTreeNode<int> *root, int val1, int val2) {

// Write your code here

if (root == NULL)
return -1;

if (root->data == val1 || root->data == val2)

return root->data;

int left = getLCA(root->left, val1, val2);

int right = getLCA(root->right, val1, val2);

if (left == -1 && right == -1)

return -1;

else if (left != -1 && right == -1)

return left;

else if (left == -1 && right != -1)

return right;

else

return root->data;

#include <climits>

#include <cmath>

class Pair {

public:

int minimum;

int maximum;

bool bst;

int height;

};

Pair BST(BinaryTreeNode<int> *root) {

if (root == NULL) {
Pair obj;

obj.minimum = INT_MAX;

obj.maximum = INT_MIN;

obj.bst = true;

obj.height = 0;

return obj;

Pair left = BST(root->left);

Pair right = BST(root->right);

int minimum = min(root->data, min(left.minimum, right.minimum));

int maximum = max(root->data, max(left.maximum, right.maximum));

bool isBSTfinal = (root->data > left.maximum) &&

(root->data < right.minimum) && left.bst && right.bst;

Pair obj;

obj.minimum = minimum;

obj.maximum = maximum;

obj.bst = isBSTfinal;

if (isBSTfinal) {

obj.height = 1 + max(left.height, right.height);

} else

obj.height = max(left.height, right.height);

return obj;

int largestBSTSubtree(BinaryTreeNode<int> *root) {

// Write your code here


return BST(root).height;

int replace(BinaryTreeNode<int> *root, int sum) {

if (root == NULL)

return sum;

sum = replace(root->right, sum);

sum = sum + root->data;

root->data = sum;

sum = replace(root->left, sum);

return sum;

void replaceWithLargerNodesSum(BinaryTreeNode<int> *root) {

if (root == NULL)

return;

int ans = replace(root, 0);

return;

#include <vector>
void rootToLeafPathsSumToK(BinaryTreeNode<int> *root, int k, vector<int> path) {
if (root == NULL)
return;
path.push_back(root->data);
k = k - root->data;
if (!root->left && !root->right) {
if (k == 0) {
for (int i : path) {
cout << i << " ";
}
cout << endl;
}
path.pop_back();
return;
}
rootToLeafPathsSumToK(root->left, k, path);
rootToLeafPathsSumToK(root->right, k, path);
}
void rootToLeafPathsSumToK(BinaryTreeNode<int> *root, int k) {
// Write your code here
vector<int> v;
rootToLeafPathsSumToK(root, k, v);
return;
}

void printkdistanceNodeDown(BinaryTreeNode<int> *root, int k) {


if (root == NULL || k < 0)
return;
if (k == 0) {
cout << root->data << endl;
return;
}
printkdistanceNodeDown(root->left, k - 1);
printkdistanceNodeDown(root->right, k - 1);
}
int printkdistanceNode(BinaryTreeNode<int> *root, int target, int k) {
if (root == NULL)
return -1;
if (root->data == target) {
printkdistanceNodeDown(root, k);
return 0;
}

int dl = printkdistanceNode(root->left, target, k);


if (dl != -1) {
if (dl + 1 == k)
cout << root->data << endl;
else
printkdistanceNodeDown(root->right, k - dl - 2);
return 1 + dl;
}
int dr = printkdistanceNode(root->right, target, k);
if (dr != -1) {
if (dr + 1 == k)
cout << root->data << endl;
else
printkdistanceNodeDown(root->left, k - dr - 2);
return 1 + dr;
}
return -1;
}
void nodesAtDistanceK(BinaryTreeNode<int> *root, int node, int k) {
int x = printkdistanceNode(root, node, k);
}

#include <stack>
int countnodes(BinaryTreeNode<int> *root) {
if (root == NULL)
return 0;

return 1 + countnodes(root->left) + countnodes(root->right);


}
void printNodesSumToS(BinaryTreeNode<int> *root, int s) {

int totalnodes = countnodes(root);


int count = 0;

stack<BinaryTreeNode<int> *> inorder;


stack<BinaryTreeNode<int> *> revinorder;

BinaryTreeNode<int> *temp = root;

while (temp) {
inorder.push(temp);

temp = temp->left;
}
temp = root;
while (temp) {
revinorder.push(temp);

temp = temp->right;
}

while (count < totalnodes - 1) {


BinaryTreeNode<int> *top1 = inorder.top();
BinaryTreeNode<int> *top2 = revinorder.top();
if (top1->data + top2->data == s) {
cout << top1->data << " " << top2->data << endl;

BinaryTreeNode<int> *top = top1;


inorder.pop();
count++;
if (top->right) {
top = top->right;
while (top) {
inorder.push(top);
top = top->left;
}
}
top = top2;
revinorder.pop();
count++;
if (top->left) {
top = top->left;
while (top) {
revinorder.push(top);
top = top->right;
}
}
} else if (top1->data + top2->data > s) {
BinaryTreeNode<int> *top = top2;
revinorder.pop();
count++;
if (top->left) {
top = top->left;
while (top) {
revinorder.push(top);
top = top->right;
}
}
} else {
BinaryTreeNode<int> *top = top1;
inorder.pop();
count++;
if (top->right) {
top = top->right;
while (top) {
inorder.push(top);
top = top->left;
}
}
}
}
}

You might also like