Wednesday, 1 Dec 2021
Category: Binary Search Tree

#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 […]

#include using namespace std;  struct TreeNode {      int data;    TreeNode* left;    TreeNode* right;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  void kDistanceDownSum(TreeNode* root,                      int k, int& sum){          if (root == NULL || k < 0)        return;          if (k == 0) {        sum += root->data;        return;    }              kDistanceDownSum(root->left,                     k – 1, sum);    kDistanceDownSum(root->right,                     k – 1, sum);}  int kDistanceSum(TreeNode* root,                 int target,                 int k, int& sum){        if (root == NULL)        return -1;          if (root->data == target) {        kDistanceDownSum(root->left,                         k – […]

#include using namespace std;  struct Node {    struct Node *left, *right;    int key;};  Node* newNode(int key){    Node* temp = new Node;    temp->key = key;    temp->left = temp->right = NULL;    return temp;}  Node* insertNode(Node* node, int key){            if (node == NULL)        return newNode(key);          if (key < node->key)        node->left = insertNode(            node->left, key);      else if (key > node->key)        node->right = insertNode(            node->right, key);          return node;}  void getIntermediateNodes(    Node* root, vector& interNodes,    int node1, int node2){        if (root == NULL)        return;          getIntermediateNodes(root->left,                         interNodes,                         node1, […]