Category: BST

Flatten a Binary Search Tree to convert the tree into a wave list in place only

  #include using namespace std;  struct TreeNode {      int data;    TreeNode* right;    TreeNode* left;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  TreeNode* insertNode(int data,                     TreeNode* root){    if (root == NULL) {        TreeNode* node            = new TreeNode(data);        return node;    }    else if (data > root->data) {        root->right = insertNode(            data, root->right);    }    else if (data data) {        root->left = insertNode(            data, root->left);    }          return root;}  int countNodes(TreeNode* root){    if (root == NULL)        return 0;      else        return countNodes(root->left)               + countNodes(root->right) + 1;}  TreeNode* toWaveList(TreeNode* root){    int […]

Continue Reading

Sum of nodes in a Binary Search Tree with values from a given range

#include using namespace std;class Node {public:    int val;    Node *left, *right;};Node* newNode(int item){    Node* temp = new Node();    temp->val = item;    temp->left = temp->right = NULL;        return temp;}int sum = 0;int rangeSumBST(Node* root, int low,                int high){        if (root == NULL)        return 0;            queue q;            q.push(root);        while (q.empty() == false) {                        Node* curr = q.front();        q.pop();                        if (curr->val >= low            && curr->val val;        }                        if (curr->left != NULL            && curr->val > low)                        q.push(curr->left);                        if (curr->right != […]

Continue Reading