Banana-Leopard/TASTY huggingface dataset:
// C++ program to count 
// full nodes in a Binary Tree
#include <bits/stdc++.h>
using namespace std;
  
// A binary tree Node has data, pointer to left
// child and a pointer to right child 
struct Node
{
    int data;
    struct Node* left, *right;
};
  
// Function to get the count of full Nodes in
// a binary tree
unsigned int getfullCount(struct Node* node)
{
    // If tree is empty
    if (!node)
        return 0;
    queue<Node *> q;
      
    // Do level order traversal starting from root
    int count = 0; // Initialize count of full nodes
    q.push(node);
    while (!q.empty())
    {
        struct Node *temp = q.front();
        q.pop();
  
        if (temp->left && temp->right)
            count++;
              
        if (temp->left != NULL)
            q.push(temp->left);
        if (temp->right != NULL)
            q.push(temp->right);
    }
    return count;
}
  
/* Helper function that allocates a new Node with the
given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
  
// Driver program 
int main(void)
{
    /* 2
     / \
    7     5
    \     \
     6     9
    / \ /
    1 11 4
    Let us create Binary Tree as shown 
    */
      
    struct Node *root = newNode(2);
    root->left     = newNode(7);
    root->right     = newNode(5);
    root->left->right = newNode(6);
    root->left->right->left = newNode(1);
    root->left->right->right = newNode(11);
    root->right->right = newNode(9);
    root->right->right->left = newNode(4);
      
    cout << getfullCount(root);
      
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count full nodes in a Binary Tree
#include <bits/stdc++.h>
using namespace std;
   
// A binary tree Node has data, pointer to left
// child and a pointer to right child
struct Node
{
    int data;
    struct Node* left, *right;
};
   
// Function to get the count of full Nodes in
// a binary tree
unsigned int getfullCount(struct Node* root)
{
    if (root == NULL)
       return 0;
   
    int res = 0;
    if  (root->left && root->right) 
       res++;
   
    res += (getfullCount(root->left) + 
            getfullCount(root->right));
    return res;
}
   
/* Helper function that allocates a new
   Node with the given data and NULL left
   and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
   
// Driver program
int main(void)
{
    /* 2
     / \
    7    5
    \    \
     6   9
    / \ /
    1 11 4
    Let us create Binary Tree as shown
    */
   
    struct Node *root = newNode(2);
    root->left    = newNode(7);
    root->right   = newNode(5);
    root->left->right = newNode(6);
    root->left->right->left = newNode(1);
    root->left->right->right = newNode(11);
    root->right->right = newNode(9);
    root->right->right->left = newNode(4);
   
    cout << getfullCount(root);
   
    return 0;
}

Time complexity: linear
Space complexity: linear
// Connect nodes at same level using level order
// traversal.
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node* left, *right, *nextRight;
};
 
// Sets nextRight of all nodes of a tree
void connect(struct Node* root)
{
    queue<Node*> q;
    q.push(root);
 
    // null marker to represent end of current level
    q.push(NULL);
 
    // Do Level order of tree using NULL markers
    while (!q.empty()) {
        Node *p = q.front();
        q.pop();
        if (p != NULL) {
 
            // next element in queue represents next
            // node at current Level
            p->nextRight = q.front();
 
            // push left and right children of current
            // node
            if (p->left)
                q.push(p->left);
            if (p->right)
                q.push(p->right);
        }
        
        // if queue is not empty, push NULL to mark
        // nodes at this level are visited
        else if (!q.empty())
            q.push(NULL);
    }
}
 
/* UTILITY FUNCTIONS */
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct Node* newnode(int data)
{
    struct Node* node = (struct Node*)
                         malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = node->nextRight = NULL;
    return (node);
}
 
/* Driver program to test above functions*/
int main()
{
 
    /* Constructed binary tree is
              10
            /   \
          8      2
        /         \
      3            90
    */
    struct Node* root = newnode(10);
    root->left = newnode(8);
    root->right = newnode(2);
    root->left->left = newnode(3);
    root->right->right = newnode(90);
 
    // Populates nextRight pointer in all nodes
    connect(root);
 
    // Let us check the values of nextRight pointers
    printf("Following are populated nextRight pointers in \n"
     "the tree (-1 is printed if there is no nextRight) \n");
    printf("nextRight of %d is %d \n", root->data,
     root->nextRight ? root->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->data,
     root->left->nextRight ? root->left->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->right->data,
     root->right->nextRight ? root->right->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->left->left->data,
     root->left->left->nextRight ? root->left->left->nextRight->data : -1);
    printf("nextRight of %d is %d \n", root->right->right->data,
     root->right->right->nextRight ? root->right->right->nextRight->data : -1);
    return 0;
}

Time complexity: linear
Space complexity: linear
/* Iterative program to connect all the adjacent nodes at
 * the same level in a binary tree*/
#include <iostream>
#include <queue>
using namespace std;
 
// A Binary Tree Node
class node {
public:
    int data;
    node* left;
    node* right;
    node* nextRight;
 
    /* Constructor that allocates a new node with the
    given data and NULL left and right pointers. */
    node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
        this->nextRight = NULL;
    }
};
// setting right pointer to next right node
/*
             10 ----------> NULL
            /  \
          8 --->2 --------> NULL
         /
        3 ----------------> NULL
        */
void connect(node* root)
{
    // Base condition
    if (root == NULL)
        return;
    // Create an empty queue like level order traversal
    queue<node*> q;
    q.push(root);
    while (!q.empty()) {
        // size indicates no. of nodes at current level
        int size = q.size();
        // for keeping track of previous node
        node* prev = NULL;
        while (size--) {
            node* temp = q.front();
            q.pop();
 
            if (temp->left)
                q.push(temp->left);
 
            if (temp->right)
                q.push(temp->right);
 
            if (prev != NULL)
                prev->nextRight = temp;
            prev = temp;
        }
        prev->nextRight = NULL;
    }
}
 
int main()
{
    /* Constructed binary tree is
           10
          /  \
        8     2
       /
      3
      */
    // Let us create binary tree shown above
    node* root = new node(10);
    root->left = new node(8);
    root->right = new node(2);
    root->left->left = new node(3);
    connect(root);
    // Let us check the values
    // of nextRight pointers
    cout << "Following are populated nextRight pointers in "
            "the tree"
            " (-1 is printed if there is no nextRight)\n";
    cout << "nextRight of " << root->data << " is "
         << (root->nextRight ? root->nextRight->data : -1)
         << endl;
    cout << "nextRight of " << root->left->data << " is "
         << (root->left->nextRight
                 ? root->left->nextRight->data
                 : -1)
         << endl;
    cout << "nextRight of " << root->right->data << " is "
         << (root->right->nextRight
                 ? root->right->nextRight->data
                 : -1)
         << endl;
    cout << "nextRight of " << root->left->left->data
         << " is "
         << (root->left->left->nextRight
                 ? root->left->left->nextRight->data
                 : -1)
         << endl;
    return 0;
}
// this code is contributed by Kapil Poonia

Time complexity: linear
Space complexity: linear
// C++ implementation to find the level
// having maximum number of Nodes
#include <bits/stdc++.h>
using namespace std;
  
/* A binary tree Node has data, pointer
   to left child and a pointer to right
   child */
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
  
/* Helper function that allocates a new node with the
   given data and NULL left and right pointers. */
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return(node);
}
  
// function to find the level
// having maximum number of Nodes
int maxNodeLevel(Node *root)
{
    if (root == NULL)
        return -1;
  
    queue<Node *> q;
    q.push(root);
  
    // Current level
    int level = 0;
  
    // Maximum Nodes at same level
    int max = INT_MIN;
  
    // Level having maximum Nodes
    int level_no = 0;
  
    while (1)
    {
        // Count Nodes in a level
        int NodeCount = q.size();
  
        if (NodeCount == 0)
            break;
  
        // If it is maximum till now
        // Update level_no to current level
        if (NodeCount > max)
        {
            max = NodeCount;
            level_no = level;
        }
  
        // Pop complete current level
        while (NodeCount > 0)
        {
            Node *Node = q.front();
            q.pop();
            if (Node->left != NULL)
                q.push(Node->left);
            if (Node->right != NULL)
                q.push(Node->right);
            NodeCount--;
        }
  
        // Increment for next level
        level++;
    }
  
    return level_no;
}
  
// Driver program to test above
int main()
{
    // binary tree formation
    struct Node *root = newNode(2);      /*        2      */
    root->left        = newNode(1);      /*      /   \    */
    root->right       = newNode(3);      /*     1     3      */
    root->left->left  = newNode(4);      /*   /   \    \  */
    root->left->right = newNode(6);      /*  4     6    8 */
    root->right->right  = newNode(8);    /*       /       */
    root->left->right->left = newNode(5);/*      5        */
  
    printf("Level having maximum number of Nodes : %d",
            maxNodeLevel(root));
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// A binary tree node has data, pointer to
// left child and a pointer to right child
struct Node {
    int val;
    struct Node *left, *right;
};
 
// Initialising a map with key as levels of the tree
map<int, pair<double, double> > mp;
 
void avg(Node* r, int l)
{
    // If the node is NULL
    if (r == NULL)
        return;
 
    // Add the current value to the sum of this level
    mp[l].first += r->val;
 
    // Increase the number of elements in the current level
    mp[l].second++;
 
    // Traverse left
    avg(r->left, l + 1);
 
    // Traverse right
    avg(r->right, l + 1);
}
void averageOfLevels(Node* root)
{
 
    avg(root, 0);
 
    // Travaersing for levels in map
    for (auto i : mp) {
        // Printing average of all levels
        cout << (i.second.first / i.second.second) << ' ';
    }
}
 
// Function to create a new node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->val = data;
    temp->left = temp->right = NULL;
    return temp;
}
int main()
{
    /* Let us construct a Binary Tree
        4
       / \
      2   9
     / \   \
    3   5   7 */
 
    Node* root = NULL;
    root = newNode(4);
    root->left = newNode(2);
    root->right = newNode(9);
    root->left->left = newNode(3);
    root->left->right = newNode(8);
    root->right->right = newNode(7);
 
    // Function Call
    averageOfLevels(root);
}
// This Code has been contributed by Alok Khansali

Time complexity: nlogn
Space complexity: logn
// C++ implementation to print largest
// value in each level of Binary Tree
#include <bits/stdc++.h>
 
using namespace std;
 
// structure of a node of binary tree
struct Node {
    int data;
    Node *left, *right;
};
 
// function to get a new node
Node* newNode(int data)
{
    // allocate space
    Node* temp = new Node;
 
    // put in the data
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// function to print largest value
// in each level of Binary Tree
void largestValueInEachLevel(Node* root)
{
    // if tree is empty
    if (!root)
        return;
 
    queue<Node*> q;
    int nc, max;
 
    // push root to the queue 'q'
    q.push(root);
 
    while (1) {
        // node count for the current level
        nc = q.size();
 
        // if true then all the nodes of
        // the tree have been traversed
        if (nc == 0)
            break;
 
        // maximum element for the current
        // level
        max = INT_MIN;
 
        while (nc--) {
 
            // get the front element from 'q'
            Node* front = q.front();
 
            // remove front element from 'q'
            q.pop();
 
            // if true, then update 'max'
            if (max < front->data)
                max = front->data;
 
            // if left child exists
            if (front->left)
                q.push(front->left);
 
            // if right child exists
            if (front->right)
                q.push(front->right);
        }
 
        // print maximum element of
        // current level
        cout << max << " ";
    }
}
 
// Driver code
int main()
{
    /* Construct a Binary Tree
        4
       / \
      9   2
     / \   \
    3   5   7 */
 
    Node* root = NULL;
    root = newNode(4);
    root->left = newNode(9);
    root->right = newNode(2);
    root->left->left = newNode(3);
    root->left->right = newNode(5);
    root->right->right = newNode(7);
 
    // Function call
    largestValueInEachLevel(root);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to Get Level of a node in a Binary Tree
#include <bits/stdc++.h>
using namespace std;
 
/* A tree node structure */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
// Helper function for getLevel(). It returns level of the
// data if data is present in tree, otherwise returns 0.
int getLevelUtil(struct node* node, int data, int level)
{
    if (node == NULL)
        return 0;
 
    if (node->data == data)
        return level;
 
    int downlevel
        = getLevelUtil(node->left, data, level + 1);
    if (downlevel != 0)
        return downlevel;
 
    downlevel = getLevelUtil(node->right, data, level + 1);
    return downlevel;
}
 
/* Returns level of given data value */
int getLevel(struct node* node, int data)
{
    return getLevelUtil(node, data, 1);
}
 
// Utility function to create a new Binary Tree node
struct node* newNode(int data)
{
    struct node* temp = new struct node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
// Driver Code
int main()
{
    struct node* root = new struct node;
    int x;
 
    // Constructing tree given in the above figure
    root = newNode(3);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(4);
 
    for (x = 1; x <= 5; x++) {
        int level = getLevel(root, x);
        if (level)
            cout << "Level of " << x << " is " << level
                 << endl;
        else
            cout << x << "is not present in tree" << endl;
    }
 
    getchar();
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program to print level in which X is present in
// binary tree using STL
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
int printLevel(Node* root, int X)
{
    Node* node;
    // Base Case
    if (root == NULL)
        return 0;
    // Create an empty queue for level order traversal
    queue<Node*> q;
    // Create a var represent current level of tree
    int currLevel = 1;
    // Enqueue Root
    q.push(root);
    while (q.empty() == false) {
        int size = q.size();
        while (size--) {
            node = q.front();
            if (node->data == X)
                return currLevel;
            q.pop();
            /* Enqueue left child */
            if (node->left != NULL)
                q.push(node->left);
            /*Enqueue right child */
            if (node->right != NULL)
                q.push(node->right);
        }
        currLevel++;
    }
    return 0;
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in above diagram
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(7);
    root->right->right = newNode(6);
 
    cout << printLevel(root, 6);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// CPP program to print level of given node
// in binary tree iterative approach
/* Example binary tree
root is at level 1
 
                20
              /   \
            10    30
           / \    / \
          5  15  25  40
             /
            12  */
#include <bits/stdc++.h>
using namespace std;
 
// node of binary tree
struct node {
    int data;
    node* left;
    node* right;
};
 
// utility function to create
// a new node
node* getnode(int data)
{
    node* newnode = new node();
    newnode->data = data;
    newnode->left = NULL;
    newnode->right = NULL;
}
 
// utility function to return level of given node
int getlevel(node* root, int data)
{
    queue<node*> q;
    int level = 1;
    q.push(root);
 
    // extra NULL is pushed to keep track
    // of all the nodes to be pushed before
    // level is incremented by 1
    q.push(NULL);
    while (!q.empty()) {
        node* temp = q.front();
        q.pop();
        if (temp == NULL) {
            if (q.front() != NULL) {
                q.push(NULL);
            }
            level += 1;
        } else {
            if (temp->data == data) {
                return level;
            }
            if (temp->left) {
                q.push(temp->left);
            }
            if (temp->right) {
                q.push(temp->right);
            }
        }
    }
    return 0;
}
 
int main()
{
    // create a binary tree
    node* root = getnode(20);
    root->left = getnode(10);
    root->right = getnode(30);
    root->left->left = getnode(5);
    root->left->right = getnode(15);
    root->left->right->left = getnode(12);
    root->right->left = getnode(25);
    root->right->right = getnode(40);
 
    // return level of node
    int level = getlevel(root, 30);
    (level != 0) ? (cout << "level of node 30 is " << level << endl) :
                   (cout << "node 30 not found" << endl);
 
    level = getlevel(root, 12);
    (level != 0) ? (cout << "level of node 12 is " << level << endl) :
                   (cout << "node 12 not found" << endl);
 
    level = getlevel(root, 25);
    (level != 0) ? (cout << "level of node 25 is " << level << endl) :
                   (cout << "node 25 not found" << endl);
 
    level = getlevel(root, 27);
    (level != 0) ? (cout << "level of node 27 is " << level << endl) :
                   (cout << "node 27 not found" << endl);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to remove all half nodes
#include <bits/stdc++.h>
using namespace std;
 
struct node
{
    int data;
    struct node* left, *right;
};
 
struct node* newNode(int data)
{
    node* nod = new node();
    nod->data = data;
    nod->left = nod->right = NULL;
    return(nod);
}
 
void printInoder(struct node*root)
{
    if (root != NULL)
    {
        printInoder(root->left);
        cout<< root->data << " ";
        printInoder(root->right);
    }
}
 
// Removes all nodes with only one child and returns
// new root (note that root may change)
struct node* RemoveHalfNodes(struct node* root)
{
    if (root==NULL)
        return NULL;
 
    root->left  = RemoveHalfNodes(root->left);
    root->right = RemoveHalfNodes(root->right);
 
    if (root->left==NULL && root->right==NULL)
        return root;
 
    /* if current nodes is a half node with left
       child NULL left, then it's right child is
       returned and replaces it in the given tree */
    if (root->left==NULL)
    {
        struct node *new_root = root->right;
        free(root); // To avoid memory leak
        return new_root;
    }
 
 
    /* if current nodes is a half node with right
       child NULL right, then it's right child is
       returned and replaces it in the given tree  */
    if (root->right==NULL)
    {
        struct node *new_root = root->left;
        free(root); // To avoid memory leak
        return new_root;
    }
 
    return root;
}
 
// Driver program
int main(void)
{
    struct node*NewRoot=NULL;
    struct node *root = newNode(2);
    root->left        = newNode(7);
    root->right       = newNode(5);
    root->left->right = newNode(6);
    root->left->right->left=newNode(1);
    root->left->right->right=newNode(11);
    root->right->right=newNode(9);
    root->right->right->left=newNode(4);
 
    cout<<"Inorder traversal of given tree \n";
    printInoder(root);
 
    NewRoot = RemoveHalfNodes(root);
 
    cout<<"\nInorder traversal of the modified tree \n";
    printInoder(NewRoot);
    return 0;
}

Time complexity: linear
Space complexity: constant