Banana-Leopard/TASTY huggingface dataset:
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
  
// A Tree node
struct Node {
  int key;
  struct Node *left, *right;
};
  
// Utility function to create a new node
Node* newNode(int key)
{
  Node* temp = new Node;
  temp->key = key;
  temp->left = temp->right = NULL;
  return (temp);
}
  
/*Function to find the height(depth) of the tree*/
int height(Node* root)
{
  
  // Initialising a variable to count the
  // height of tree
  queue<Node*> q;
  q.push(root);
  int height = 0;
  while (!q.empty()) {
    int size = q.size();
    for (int i = 0; i < size; i++) {
      Node* temp = q.front();
      q.pop();
      if (temp->left != NULL) {
        q.push(temp->left);
      }
      if (temp->right != NULL) {
        q.push(temp->right);
      }
    }
    height++;
  }
  return height;
}
  
// Driver program
int main()
{
  
  // Let us create Binary Tree shown in above example
  Node* root = newNode(1);
  root->left = newNode(2);
  root->right = newNode(3);
  
  root->left->left = newNode(4);
  root->left->right = newNode(5);
  
  cout << "Height(Depth) of tree is: " << height(root);
}
  
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
#include <iostream>
#include <queue>
 
using namespace std;
 
// This approach counts the number of nodes from root to the
// leaf to calculate the height of the tree.
 
// Defining the structure of a Node.
 
class Node {
public:
    int data;
    Node* left;
    Node* right;
};
 
// Helper function to create a newnode.
// Input: Data for the newnode.
// Return: Address of the newly created node.
 
Node* createNode(int data)
{
 
    Node* newnode = new Node();
    newnode->data = data;
    newnode->left = NULL;
    newnode->right = NULL;
 
    return newnode;
}
 
// Function to calculate the height of given Binary Tree.
// Input: Address of the root node of Binary Tree.
// Return: Height of Binary Tree as a integer. This includes
// the number of nodes from root to the leaf.
 
int calculateHeight(Node* root)
{
    queue<Node*> nodesInLevel;
    int height = 0;
    int nodeCount = 0; // Calculate  number of nodes in a level.
    Node* currentNode; // Pointer to store the address of a
                       // node in the current level.
    if (root == NULL) {
        return 0;
    }
    nodesInLevel.push(root);
    while (!nodesInLevel.empty()) {
        // This while loop runs for every level and
        // increases the height by 1 in each iteration. If
        // the queue is empty then it implies that the last
        // level of tree has been parsed.
        height++;
        // Create another while loop which will insert all
        // the child nodes of the current level in the
        // queue.
 
        nodeCount = nodesInLevel.size();
        while (nodeCount--) {
            currentNode = nodesInLevel.front();
 
            // Check if the current nodes has left child and
            // insert it in the queue.
 
            if (currentNode->left != NULL) {
                nodesInLevel.push(currentNode->left);
            }
 
            // Check if the current nodes has right child
            // and insert it in the queue.
            if (currentNode->right != NULL) {
                nodesInLevel.push(currentNode->right);
            }
 
            // Once the children of the current node are
            // inserted. Delete the current node.
 
            nodesInLevel.pop();
        }
    }
    return height;
}
 
// Driver Function.
 
int main()
{
    // Creating a binary tree.
 
    Node* root = NULL;
 
    root = createNode(1);
    root->left = createNode(2);
    root->left->left = createNode(4);
    root->left->right = createNode(5);
    root->right = createNode(3);
 
    cout << "The height of the binary tree using iterative "
            "method is: " << calculateHeight(root) << ".";
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find height of complete
// binary tree from total nodes.
#include <bits/stdc++.h>
using namespace std;
 
int height(int N)
{
    return floor(log2(N));
}
 
// driver node
int main()
{
    int N = 2;
    cout << height(N);
    return 0;
}

Time complexity: constant
Space complexity: constant
/* Program to find height of the tree considering
   only even level leaves. */
#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;
};
 
int heightOfTreeUtil(Node* root, bool isEven)
{
    // Base Case
    if (!root)
        return 0;
 
    if (!root->left && !root->right) {
        if (isEven)
            return 1;
        else
            return 0;
    }
 
    /*left stores the result of left subtree,
      and right stores the result of right subtree*/
    int left = heightOfTreeUtil(root->left, !isEven);
    int right = heightOfTreeUtil(root->right, !isEven);
 
    /*If both left and right returns 0, it means
      there is no valid path till leaf node*/
    if (left == 0 && right == 0)
        return 0;
 
    return (1 + max(left, 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 =
              (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
int heightOfTree(Node* root)
{
    return heightOfTreeUtil(root, false);
}
 
/* Driver program to test above functions*/
int main()
{
    // Let us create binary tree shown in above diagram
    struct Node* root = newNode(1);
 
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->left->right->left = newNode(6);
    cout << "Height of tree is " << heightOfTree(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// Recursive optimized C program to find the diameter of 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 create a new node of tree and returns pointer
struct node* newNode(int data);
 
// returns max of two integers
int max(int a, int b) { return (a > b) ? a : b; }
 
// function to Compute height of a tree.
int height(struct node* node);
 
// Function to get diameter of a binary tree
int diameter(struct node* tree)
{
    // base case where tree is empty
    if (tree == NULL)
        return 0;
 
    // get the height of left and right sub-trees
    int lheight = height(tree->left);
    int rheight = height(tree->right);
 
    // get the diameter of left and right sub-trees
    int ldiameter = diameter(tree->left);
    int rdiameter = diameter(tree->right);
 
    // Return max of following three
    // 1) Diameter of left subtree
    // 2) Diameter of right subtree
    // 3) Height of left subtree + height of right subtree +
    // 1
    return max(lheight + rheight + 1,
               max(ldiameter, rdiameter));
}
 
// UTILITY FUNCTIONS TO TEST diameter() FUNCTION
 
// The function Compute the "height" of a tree. Height is
// the number f nodes along the longest path from the root
// node down to the farthest leaf node.
int height(struct node* node)
{
    // base case tree is empty
    if (node == NULL)
        return 0;
 
    // If tree is not empty then height = 1 + max of left
    // height and right heights
    return 1 + max(height(node->left), height(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
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
// Driver Code
int main()
{
 
    /* Constructed binary tree is
            1
            / \
        2     3
        / \
    4     5
    */
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
 
    // Function Call
    cout << "Diameter of the given binary tree is "
         << diameter(root);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

Time complexity: quadratic
Space complexity: linear
// Recursive optimized C++ program to find the diameter of 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 create a new node of tree and returns pointer
struct node* newNode(int data);
 
int diameterOpt(struct node* root, int* height)
{
    // lh --> Height of left subtree
    // rh --> Height of right subtree
    int lh = 0, rh = 0;
 
    // ldiameter  --> diameter of left subtree
    // rdiameter  --> Diameter of right subtree
    int ldiameter = 0, rdiameter = 0;
 
    if (root == NULL) {
        *height = 0;
        return 0; // diameter is also 0
    }
 
    // Get the heights of left and right subtrees in lh and
    // rh And store the returned values in ldiameter and
    // ldiameter
    ldiameter = diameterOpt(root->left, &lh);
    rdiameter = diameterOpt(root->right, &rh);
 
    // Height of current node is max of heights of left and
    // right subtrees plus 1
    *height = max(lh, rh) + 1;
 
    return max(lh + rh + 1, max(ldiameter, rdiameter));
}
 
// 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 = NULL;
    node->right = NULL;
 
    return (node);
}
 
// Driver Code
int main()
{
 
    /* Constructed binary tree is
            1
            / \
        2     3
        / \
    4     5
    */
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
 
    int height = 0;
 
    // Function Call
    cout << "Diameter of the given binary tree is "
         << diameterOpt(root, &height);
 
    return 0;
}
 
// This code is contributed by probinsah.

Time complexity: linear
Space complexity: linear
// Simple C++ program to find diameter
// of a binary tree.
#include <bits/stdc++.h>
using namespace std;
 
/* Tree node structure used in the program */
struct Node {
    int data;
    Node* left, *right;
};
 
/* Function to find height of a tree */
int height(Node* root, int& ans)
{
    if (root == NULL)
        return 0;
 
    int left_height = height(root->left, ans);
 
    int right_height = height(root->right, ans);
 
    // update the answer, because diameter of a
    // tree is nothing but maximum value of
    // (left_height + right_height + 1) for each node
    ans = max(ans, 1 + left_height + right_height);
 
    return 1 + max(left_height, right_height);
}
 
/* Computes the diameter of binary tree with given root. */
int diameter(Node* root)
{
    if (root == NULL)
        return 0;
    int ans = INT_MIN; // This will store the final answer
    height(root, ans);
    return ans;
}
 
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
 
    return (node);
}
 
// Driver code
int main()
{
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
 
    printf("Diameter is %d\n", diameter(root));
 
    return 0;
}

Time complexity: linear
Space complexity: logn
// CPP program to find deepest right leaf
// node of binary tree
 
#include <bits/stdc++.h>
 
using namespace std;
 
// tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// returns a new tree Node
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// return the deepest right leaf node
// of binary tree
Node* getDeepestRightLeafNode(Node* root)
{
    if (!root)
        return NULL;
 
    // create a queue for level order traversal
    queue<Node*> q;
    q.push(root);
 
    Node* result = NULL;
 
    // traverse until the queue is empty
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
 
        if (temp->left) {
            q.push(temp->left);
        }
 
        // Since we go level by level, the last
        // stored right leaf node is deepest one
        if (temp->right) {
            q.push(temp->right);
            if (!temp->right->left && !temp->right->right)
                result = temp->right;
        }
    }
    return result;
}
 
// driver program
int main()
{
    // construct a tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->right = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(6);
    root->right->left->right = newNode(7);
    root->right->right->right = newNode(8);
    root->right->left->right->left = newNode(9);
    root->right->right->right->right = newNode(10);
 
    Node* result = getDeepestRightLeafNode(root);
    if (result)
        cout << "Deepest Right Leaf Node :: "
             << result->data << endl;
    else
        cout << "No result, right leaf not found\n";
    return 0;
}

Time complexity: linear
Space complexity: linear
// A C++ program to find value of the deepest node
// in a given binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A tree node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// Utility function to create a new node
Node *newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// maxLevel : keeps track of maximum level seen so far.
// res :  Value of deepest node so far.
// level : Level of root
void find(Node *root, int level, int &maxLevel, int &res)
{
    if (root != NULL)
    {
        find(root->left, ++level, maxLevel, res);
 
        // Update level and rescue
        if (level > maxLevel)
        {
            res = root->data;
            maxLevel = level;
        }
 
        find(root->right, level, maxLevel, res);
    }
}
 
// Returns value of deepest node
int deepestNode(Node *root)
{
    // Initialize result and max level
    int res = -1;
    int maxLevel = -1;
 
    // Updates value "res" and "maxLevel"
    // Note that res and maxLen are passed
    // by reference.
    find(root, 0, maxLevel, res);
    return res;
}
 
// Driver program
int main()
{
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(6);
    root->right->left->right = newNode(7);
    root->right->right->right = newNode(8);
    root->right->left->right->left = newNode(9);
    cout << deepestNode(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// A C++ program to find value of the
// deepest node in a given binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A tree node with constructor
class Node
{
public:
    int data;
    Node *left, *right;
      
    // constructor  
    Node(int key)
    {
        data = key;
        left = NULL;
        right = NULL;
    }
};
 
// Function to return the deepest node
Node* deepestNode(Node* root)
{
    Node* tmp = NULL;
    if (root == NULL)
        return NULL;
 
    // Creating a Queue
    queue<Node*> q;
    q.push(root);
 
    // Iterates until queue become empty
    while (q.size() > 0)
    {
        tmp = q.front();
        q.pop();
        if (tmp->left != NULL)
            q.push(tmp->left);
        if (tmp->right != NULL)
            q.push(tmp->right);
    }
    return tmp;
}
     
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->right->left = new Node(5);
    root->right->right = new Node(6);
    root->right->left->right = new Node(7);
    root->right->right->right = new Node(8);
    root->right->left->right->left = new Node(9);
 
    Node* deepNode = deepestNode(root);
    cout << (deepNode->data);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find deepest left leaf
// node of binary tree
#include <bits/stdc++.h>
using namespace std;
 
// tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// returns a new tree Node
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// return the deepest left leaf node
// of binary tree
Node* getDeepestLeftLeafNode(Node* root)
{
    if (!root)
        return NULL;
 
    // create a queue for level order traversal
    queue<Node*> q;
    q.push(root);
 
    Node* result = NULL;
 
    // traverse until the queue is empty
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
 
          
        // Since we go level by level, the last
        // stored left leaf node is deepest one,
        if (temp->left) {
            q.push(temp->left);
            if (!temp->left->left && !temp->left->right)
                result = temp->left;
        }
         
        if (temp->right)
            q.push(temp->right);
    }
    return result;
}
 
// driver program
int main()
{
    // construct a tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(6);
    root->right->left->right = newNode(7);
    root->right->right->right = newNode(8);
    root->right->left->right->left = newNode(9);
    root->right->right->right->right = newNode(10);
 
    Node* result = getDeepestLeftLeafNode(root);
    if (result)
        cout << "Deepest Left Leaf Node :: "
             << result->data << endl;
    else
        cout << "No result, left leaf not found\n";
    return 0;
}

Time complexity: linear
Space complexity: linear
// 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;
    }
};
 
/*Function prototypes*/
int getWidth(node* root, int level);
int height(node* node);
 
/* Function to get the maximum width of a binary tree*/
int getMaxWidth(node* root)
{
    int maxWidth = 0;
    int width;
    int h = height(root);
    int i;
 
    /* Get width of each level and compare
        the width with maximum width so far */
    for (i = 1; i <= h; i++) {
        width = getWidth(root, i);
        if (width > maxWidth)
            maxWidth = width;
    }
 
    return maxWidth;
}
 
/* Get width of a given level */
int getWidth(node* root, int level)
{
    if (root == NULL)
        return 0;
    if (level == 1)
        return 1;
    else if (level > 1)
        return getWidth(root->left, level - 1)
               + getWidth(root->right, 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);
    }
}
 
/* 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);
 
    /*
    Constructed binary tree is:
            1
            / \
        2 3
        / \ \
        4 5 8
                / \
                6 7
    */
 
    // Function call
    cout << "Maximum width is " << getMaxWidth(root)
         << endl;
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: constant
// A queue based C++ program to find maximum width
// of 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;
    struct Node* right;
    Node(int d)
    {
        this->data = d;
        this->left = this->right = NULL;
    }
};
 
// Function to find the maximum width of the tree
// using level order traversal
int maxWidth(struct Node* root)
{
    // Base case
    if (root == NULL)
        return 0;
 
    // Initialize result
    int result = 0;
 
    // Do Level order traversal keeping track of number
    // of nodes at every level.
    queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        // Get the size of queue when the level order
        // traversal for one level finishes
        int count = q.size();
 
        // Update the maximum node count value
        result = max(count, result);
 
        // Iterate for all the nodes in the queue currently
        while (count--) {
            // Dequeue an node from queue
            Node* temp = q.front();
            q.pop();
 
            // Enqueue left and right children of
            // dequeued node
            if (temp->left != NULL)
                q.push(temp->left);
            if (temp->right != NULL)
                q.push(temp->right);
        }
    }
 
    return result;
}
 
// Driver code
int main()
{
    struct 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);
 
    /*   Constructed Binary tree is:
                 1
               /   \
             2      3
           /  \      \
          4    5      8
                    /   \
                   6     7    */
 
    // Function call
    cout << "Maximum width is " << maxWidth(root) << endl;
    return 0;
}
 
// This code is contributed by Nikhil Kumar
// Singh(nickzuck_007)

Time complexity: linear
Space complexity: np