Searchinbst Binarytreenode Root K Root Null: Bool If Return False
Searchinbst Binarytreenode Root K Root Null: Bool If Return False
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
if(root->data == k){
return true;
}
if (root->data < k) {
// element is present at the right side
bool ans2 = searchInBST(root->right, k);
return ans2;
}
}
/**********************************************************
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
}
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
BinaryTreeNode(T data) {
this -> data = data;
left = NULL;
right = NULL;
}
};
***********************************************************/
#include<climits>
//Now attach the left and the right subtree with the root node
root->left = leftTree;
root->right = rightTree;
return root;
Pair output;
return output;
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 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
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;
}
}
}
void print() {
// Implement the print() function
}
=====================================================================================
void insertDuplicateNode(BinaryTreeNode<int> *root) {
convert(root, arr);
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++;
}
}
if (root == NULL)
return -1;
return root->data;
return -1;
return left;
return right;
else
return root->data;
#include <climits>
#include <cmath>
class Pair {
public:
int minimum;
int maximum;
bool bst;
int height;
};
if (root == NULL) {
Pair obj;
obj.minimum = INT_MAX;
obj.maximum = INT_MIN;
obj.bst = true;
obj.height = 0;
return obj;
Pair obj;
obj.minimum = minimum;
obj.maximum = maximum;
obj.bst = isBSTfinal;
if (isBSTfinal) {
} else
return obj;
if (root == NULL)
return sum;
root->data = sum;
return sum;
if (root == NULL)
return;
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;
}
#include <stack>
int countnodes(BinaryTreeNode<int> *root) {
if (root == NULL)
return 0;
while (temp) {
inorder.push(temp);
temp = temp->left;
}
temp = root;
while (temp) {
revinorder.push(temp);
temp = temp->right;
}