Banana-Leopard/TASTY huggingface dataset:
// C++ program to calculate width of 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 */
class node {
public:
    int data;
    node* left;
    node* right;
    node(int d)
    {
        this->data = d;
        this->left = this->right = NULL;
    }
};
 
// A utility function to get
// height of a binary tree
int height(node* node);
 
// A utility function that returns
// maximum value in arr[] of size n
int getMax(int arr[], int n);
 
// A function that fills count array
// with count of nodes at every
// level of given binary tree
void getMaxWidthRecur(node* root, int count[], int level);
 
/* Function to get the maximum
width of a binary tree*/
int getMaxWidth(node* root)
{
    int width;
    int h = height(root);
 
    // Create an array that will
    // store count of nodes at each level
    int* count = new int[h];
 
    int level = 0;
 
    // Fill the count array using preorder traversal
    getMaxWidthRecur(root, count, level);
 
    // Return the maximum value from count array
    return getMax(count, h);
}
 
// A function that fills count array
// with count of nodes at every
// level of given binary tree
void getMaxWidthRecur(node* root,
                      int count[], int level)
{
    if (root) {
        count[level]++;
        getMaxWidthRecur(root->left, count, level + 1);
        getMaxWidthRecur(root->right, count, level + 1);
    }
}
 
/* UTILITY FUNCTIONS */
/* Compute the "height" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
int height(node* node)
{
    if (node == NULL)
        return 0;
    else {
        /* compute the height of each subtree */
        int lHeight = height(node->left);
        int rHeight = height(node->right);
        /* use the larger one */
 
        return (lHeight > rHeight) ? (lHeight + 1)
                                   : (rHeight + 1);
    }
}
 
// Return the maximum value from count array
int getMax(int arr[], int n)
{
    int max = arr[0];
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}
 
/* Driver code*/
int main()
{
    node* root = new node(1);
    root->left = new node(2);
    root->right = new node(3);
    root->left->left = new node(4);
    root->left->right = new node(5);
    root->right->right = new node(8);
    root->right->right->left = new node(6);
    root->right->right->right = new node(7);
 
    cout << "Maximum width is " << getMaxWidth(root)
         << endl;
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: logn
// CPP program to print vertical width
// of a tree
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
// get vertical width
void lengthUtil(Node* root, int& maximum, int& minimum,
                int curr = 0)
{
    if (root == NULL)
        return;
 
    // traverse left
    lengthUtil(root->left, maximum, minimum, curr - 1);
 
    // if curr is decrease then get
    // value in minimum
    if (minimum > curr)
        minimum = curr;
 
    // if curr is increase then get
    // value in maximum
    if (maximum < curr)
        maximum = curr;
 
    // traverse right
    lengthUtil(root->right, maximum, minimum, curr + 1);
}
 
int getLength(Node* root)
{
    int maximum = 0, minimum = 0;
    lengthUtil(root, maximum, minimum, 0);
 
    // 1 is added to include root in the width
    return (abs(minimum) + maximum) + 1;
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node* curr = new Node;
    curr->data = data;
    curr->left = curr->right = NULL;
    return curr;
}
 
// Driver program to test above functions
int main()
{
 
    Node* root = newNode(7);
    root->left = newNode(6);
    root->right = newNode(5);
    root->left->left = newNode(4);
    root->left->right = newNode(3);
    root->right->left = newNode(2);
    root->right->right = newNode(1);
 
    cout << getLength(root) << "\n";
 
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to find Vertical Height of a tree
 
#include <bits/stdc++.h>
#include <iostream>
 
using namespace std;
 
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
 
    Node(int x)
    {
        data = x;
        left = right = NULL;
    }
};
 
int verticalWidth(Node* root)
{
    // Code here
    if (root == NULL)
        return 0;
 
    int maxLevel = 0, minLevel = 0;
    queue<pair<Node*, int> > q;
    q.push({ root, 0 }); // we take root as 0
 
    // Level order traversal code
    while (q.empty() != true) {
        Node* cur = q.front().first;
        int count = q.front().second;
        q.pop();
 
        if (cur->left) {
            minLevel = min(minLevel,
                           count - 1); // as we go left,
                                       // level is decreased
            q.push({ cur->left, count - 1 });
        }
 
        if (cur->right) {
            maxLevel = max(maxLevel,
                           count + 1); // as we go right,
                                       // level is increased
            q.push({ cur->right, count + 1 });
        }
    }
 
    return maxLevel + abs(minLevel)
           + 1; // +1 for the root node, we gave it a value
                // of zero
}
 
int main()
{
    // making the tree
    Node* root = new Node(7);
    root->left = new Node(6);
    root->right = new Node(5);
    root->left->left = new Node(4);
    root->left->right = new Node(3);
    root->right->left = new Node(2);
    root->right->right = new Node(1);
 
    cout << "Vertical width is : " << verticalWidth(root)
         << endl;
    return 0;
}
 
// code contributed by Anshit Bansal

Time complexity: linear
Space complexity: linear
// CPP code to find vertical
// width of a binary tree
#include <bits/stdc++.h>
using namespace std;
 
// Tree class
class Node
{
public :
    int data;
    Node *left, *right;
 
    // Constructor
    Node(int data_new)
    {
        data = data_new;
        left = right = NULL;
    }
};
 
// Function to fill hd in set.
void fillSet(Node* root, unordered_set<int>& s,
                                       int hd)
{
    if (!root)
        return;
 
    fillSet(root->left, s, hd - 1);
    s.insert(hd);
    fillSet(root->right, s, hd + 1);
}
 
int verticalWidth(Node* root)
{
    unordered_set<int> s;
 
    // Third parameter is horizontal
    // distance
    fillSet(root, s, 0);
 
    return s.size();
}
 
int main()
{
    Node* root = NULL;
 
    // Creating the above tree
    root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    root->right->left = new Node(6);
    root->right->right = new Node(7);
    root->right->left->right = new Node(8);
    root->right->right->right = new Node(9);
 
    cout << verticalWidth(root) << "\n";
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to determine whether
// vertical level l of binary tree
// is sorted or not.
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a tree node.
struct Node {
    int key;
    Node *left, *right;
};
 
// Function to create new tree node.
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Helper function to determine if
// vertical level l of given binary
// tree is sorted or not.
bool isSorted(Node* root, int level)
{
    // If root is null, then the answer is an
    // empty subset and an empty subset is
    // always considered to be sorted.
    if (root == NULL)
        return true;
     
    // Variable to store previous
    // value in vertical level l.
    int prevVal = INT_MIN;
 
    // Variable to store current level
    // while traversing tree vertically.
    int currLevel;
 
    // Variable to store current node
    // while traversing tree vertically.
    Node* currNode;
 
    // Declare queue to do vertical order
    // traversal. A pair is used as element
    // of queue. The first element in pair
    // represents the node and the second
    // element represents vertical level
    // of that node.
    queue<pair<Node*, int> > q;
 
    // Insert root in queue. Vertical level
    // of root is 0.
    q.push(make_pair(root, 0));
 
    // Do vertical order traversal until
    // all the nodes are not visited.
    while (!q.empty()) {
        currNode = q.front().first;
        currLevel = q.front().second;
        q.pop();
 
        // Check if level of node extracted from
        // queue is required level or not. If it
        // is the required level then check if
        // previous value in that level is less
        // than or equal to value of node.
        if (currLevel == level) {
            if (prevVal <= currNode->key)
                prevVal = currNode->key;           
            else
                return false;           
        }
 
        // If left child is not NULL then push it
        // in queue with level reduced by 1.
        if (currNode->left)
            q.push(make_pair(currNode->left, currLevel - 1));
 
        // If right child is not NULL then push it
        // in queue with level increased by 1.
        if (currNode->right)
            q.push(make_pair(currNode->right, currLevel + 1));
    }
 
    // If the level asked is not present in the
    // given binary tree, that means that level
    // will contain an empty subset. Therefore answer
    // will be true.
    return true;
}
 
// Driver program
int main()
{
    /*
             1
            / \
           2   5
          / \
         7   4
            /
           6
    */
 
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(7);
    root->left->right = newNode(4);
    root->left->right->left = newNode(6);
 
    int level = -1;
    if (isSorted(root, level) == true)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to determine whether
// binary tree is level sorted or not.
 
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a tree node.
struct Node {
    int key;
    Node *left, *right;
};
 
// Function to create new tree node.
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Function to determine if
// given binary tree is level sorted
// or not.
int isSorted(Node* root)
{
 
    // to store maximum value of previous
    // level.
    int prevMax = INT_MIN;
 
    // to store minimum value of current
    // level.
    int minval;
 
    // to store maximum value of current
    // level.
    int maxval;
 
    // to store number of nodes in current
    // level.
    int levelSize;
 
    // queue to perform level order traversal.
    queue<Node*> q;
    q.push(root);
 
    while (!q.empty()) {
 
        // find number of nodes in current
        // level.
        levelSize = q.size();
 
        minval = INT_MAX;
        maxval = INT_MIN;
 
        // traverse current level and find
        // minimum and maximum value of
        // this level.
        while (levelSize > 0) {
            root = q.front();
            q.pop();
 
            levelSize--;
 
            minval = min(minval, root->key);
            maxval = max(maxval, root->key);
 
            if (root->left)
                q.push(root->left);
 
            if (root->right)
                q.push(root->right);
        }
 
        // if minimum value of this level
        // is not greater than maximum
        // value of previous level then
        // given tree is not level sorted.
        if (minval <= prevMax)
            return 0;
 
        // maximum value of this level is
        // previous maximum value for
        // next level.
        prevMax = maxval;
    }
 
    return 1;
}
 
// Driver program
int main()
{
    /*
            1
           /
          4  
           \
            6
           / \
          8   9
         /     \
        12     10
    */
 
    Node* root = newNode(1);
    root->left = newNode(4);
    root->left->right = newNode(6);
    root->left->right->left = newNode(8);
    root->left->right->right = newNode(9);
    root->left->right->left->left = newNode(12);
    root->left->right->right->right = newNode(10);
 
    if (isSorted(root))
        cout << "Sorted";
    else
        cout << "Not sorted";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ Program to print Bottom View of Binary Tree
#include<bits/stdc++.h>
using namespace std;
 
// Tree node class
struct Node
{
    int data; //data of the node
    int hd; //horizontal distance of the node
    Node *left, *right; //left and right references
 
    // Constructor of tree node
    Node(int key)
    {
        data = key;
        hd = INT_MAX;
        left = right = NULL;
    }
};
 
// Method that prints the bottom view.
void bottomView(Node *root)
{
    if (root == NULL)
        return;
 
    // Initialize a variable 'hd' with 0
    // for the root element.
    int hd = 0;
 
    // TreeMap which stores key value pair
    // sorted on key value
    map<int, int> m;
 
    // Queue to store tree nodes in level
    // order traversal
    queue<Node *> q;
 
    // Assign initialized horizontal distance
    // value to root node and add it to the queue.
    root->hd = hd;
    q.push(root);  // In STL, push() is used enqueue an item
 
    // Loop until the queue is empty (standard
    // level order loop)
    while (!q.empty())
    {
        Node *temp = q.front();
        q.pop();   // In STL, pop() is used dequeue an item
 
        // Extract the horizontal distance value
        // from the dequeued tree node.
        hd = temp->hd;
 
        // Put the dequeued tree node to TreeMap
        // having key as horizontal distance. Every
        // time we find a node having same horizontal
        // distance we need to replace the data in
        // the map.
        m[hd] = temp->data;
 
        // If the dequeued node has a left child, add
        // it to the queue with a horizontal distance hd-1.
        if (temp->left != NULL)
        {
            temp->left->hd = hd-1;
            q.push(temp->left);
        }
 
        // If the dequeued node has a right child, add
        // it to the queue with a horizontal distance
        // hd+1.
        if (temp->right != NULL)
        {
            temp->right->hd = hd+1;
            q.push(temp->right);
        }
    }
 
    // Traverse the map elements using the iterator.
    for (auto i = m.begin(); i != m.end(); ++i)
        cout << i->second << " ";
}
 
// Driver Code
int main()
{
    Node *root = new Node(20);
    root->left = new Node(8);
    root->right = new Node(22);
    root->left->left = new Node(5);
    root->left->right = new Node(3);
    root->right->left = new Node(4);
    root->right->right = new Node(25);
    root->left->right->left = new Node(10);
    root->left->right->right = new Node(14);
    cout << "Bottom view of the given binary tree :\n";
    bottomView(root);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// C++ Program to print Bottom View of Binary Tree
#include <bits/stdc++.h>
#include <map>
using namespace std;
 
// Tree node class
struct Node
{
    // data of the node
    int data;
     
    // horizontal distance of the node
    int hd;
     
    //left and right references
    Node * left, * right;
     
    // Constructor of tree node
    Node(int key)
    {
        data = key;
        hd = INT_MAX;
        left = right = NULL;
    }
};
 
void printBottomViewUtil(Node * root, int curr, int hd, map <int, pair <int, int>> & m)
{
    // Base case
    if (root == NULL)
        return;
     
    // If node for a particular
    // horizontal distance is not
    // present, add to the map.
    if (m.find(hd) == m.end())
    {
        m[hd] = make_pair(root -> data, curr);
    }
    // Compare height for already
    // present node at similar horizontal
    // distance
    else
    {
        pair < int, int > p = m[hd];
        if (p.second <= curr)
        {
            m[hd].second = curr;
            m[hd].first = root -> data;
        }
    }
     
    // Recur for left subtree
    printBottomViewUtil(root -> left, curr + 1, hd - 1, m);
     
    // Recur for right subtree
    printBottomViewUtil(root -> right, curr + 1, hd + 1, m);
}
 
void printBottomView(Node * root)
{
     
    // Map to store Horizontal Distance,
    // Height and Data.
    map < int, pair < int, int > > m;
     
    printBottomViewUtil(root, 0, 0, m);
     
     // Prints the values stored by printBottomViewUtil()
    map < int, pair < int, int > > ::iterator it;
    for (it = m.begin(); it != m.end(); ++it)
    {
        pair < int, int > p = it -> second;
        cout << p.first << " ";
    }
}
 
int main()
{
    Node * root = new Node(20);
    root -> left = new Node(8);
    root -> right = new Node(22);
    root -> left -> left = new Node(5);
    root -> left -> right = new Node(3);
    root -> right -> left = new Node(4);
    root -> right -> right = new Node(25);
    root -> left -> right -> left = new Node(10);
    root -> left -> right -> right = new Node(14);
    cout << "Bottom view of the given binary tree :\n";
    printBottomView(root);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// Tree node class
struct Node
{
    // data of the node
    int data;
    // horizontal distance of the node
    int hd;
    //left and right references
    Node * left, * right;
    // Constructor of tree node
    Node(int key)
    {
        data = key;
        hd = INT_MAX;
        left = right = NULL;
    }
};
 
 
void printBottomView(Node * root)
{
    if(!root) return ;//if root is NULL
    unordered_map<int,int> hash; // <vertical_index , root->data>
    int leftmost = 0; // to store the leftmost index so that we move from left to right
    queue<pair<Node*,int>> q; // pair<Node*,vertical Index>  for level order traversal.
    q.push({root,0}); // push the root and 0 vertial index
    while(!q.empty()){
        auto top = q.front(); //  store q.front() in top variable
        q.pop();
        Node *temp = top.first; // store the Node in temp for left and right nodes
        int ind = top.second; // store the vertical index of current node
        hash[ind] = temp->data; // store the latest vertical_index(key) -> root->data(value)
        leftmost = min(ind,leftmost); // have the leftmost vertical index
        if(temp->left){ q.push({temp->left,ind-1});}// check if any node of left then go in negative direction
        if(temp->right){ q.push({temp->right,ind+1});} //check if any node of left then go in positive direction
    }
    //teverse each value in hash from leftmost to positive side till key is available
    while(hash.find(leftmost)!=hash.end()){ cout<<hash[leftmost++]<<" "; }
}
 
int main()
{
    Node * root = new Node(20);
    root -> left = new Node(8);
    root -> right = new Node(22);
    root -> left -> left = new Node(5);
    root -> left -> right = new Node(3);
    root -> right -> left = new Node(4);
    root -> right -> right = new Node(25);
    root -> left -> right -> left = new Node(10);
    root -> left -> right -> right = new Node(14);
    cout << "Bottom view of the given binary tree :\n";
    printBottomView(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count half 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 half Nodes in
// a binary tree
unsigned int gethalfCount(struct Node* node)
{
    // If tree is empty
    if (!node)
        return 0;
  
    int count = 0; // Initialize count of half nodes
  
    // Do level order traversal starting from root
    queue<Node *> q;
    q.push(node);
    while (!q.empty())
    {
        struct Node *temp = q.front();
        q.pop();
  
        if (!temp->left && temp->right ||
            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 shown in
    above example */
  
    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 << gethalfCount(root);
  
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count half 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 half Nodes in
// a binary tree
unsigned int gethalfCount(struct Node* root)
{
    if (root == NULL)
       return 0;
  
    int res = 0;
    if  ((root->left == NULL && root->right != NULL) ||
         (root->left != NULL && root->right == NULL))
       res++;
  
    res += (gethalfCount(root->left) + gethalfCount(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 shown in
    above example */
  
    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 << gethalfCount(root);
  
    return 0;
}

Time complexity: linear
Space complexity: linear