Banana-Leopard/TASTY huggingface dataset:
// C++ implementation to find the sum of
// leaf nodes at minimum level
#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* getNode(int data)
{
    // allocate space
    Node* newNode = (Node*)malloc(sizeof(Node));
 
    // put in the data
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
// function to find the sum of
// leaf nodes at minimum level
int sumOfLeafNodesAtMinLevel(Node* root)
{
    // if tree is empty
    if (!root)
        return 0;
 
    // if there is only one node
    if (!root->left && !root->right)
        return root->data;
 
    // queue used for level order traversal
    queue<Node*> q;
    int sum = 0;
    bool f = 0;
 
    // push root node in the queue 'q'
    q.push(root);
 
    while (f == 0) {
 
        // count number of nodes in the
        // current level
        int nc = q.size();
 
        // traverse the current level nodes
        while (nc--) {
 
            // get front element from 'q'
            Node* top = q.front();
            q.pop();
 
            // if it is a leaf node
            if (!top->left && !top->right) {
 
                // accumulate data to 'sum'
                sum += top->data;
 
                // set flag 'f' to 1, to signify
                // minimum level for leaf nodes
                // has been encountered
                f = 1;
            }
            else {
 
                // if top's left and right child
                // exists, then push them to 'q'
                if (top->left)
                    q.push(top->left);
                if (top->right)
                    q.push(top->right);
            }
        }
    }
 
    // required sum
    return sum;
}
 
// Driver program to test above
int main()
{
    // binary tree creation
    Node* root = getNode(1);
    root->left = getNode(2);
    root->right = getNode(3);
    root->left->left = getNode(4);
    root->left->right = getNode(5);
    root->right->left = getNode(6);
    root->right->right = getNode(7);
    root->left->right->left = getNode(8);
    root->right->left->right = getNode(9);
 
    cout << "Sum = "
         << sumOfLeafNodesAtMinLevel(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    Node *left, *right;
};
 
// function to get a new node
Node* getNode(int data)
{
    // allocate space
    Node* newNode = (Node*)malloc(sizeof(Node));
 
    // put in the data
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
 
    map <int, vector <int>> mp;
    void solve(Node* root, int level) {
        if(root == NULL)   
            return;
        if(root->left == NULL && root->right == NULL)
            mp[level].push_back(root->data);
        solve(root->left, level+1);
        solve(root->right, level+1);
    }
    int minLeafSum(Node *root)
    {
        solve(root, 0);
        int sum = 0;
        for(auto i:mp) {
            for(auto j:i.second) {
                sum += j;
            }
            return sum;
        }
    }
 
int main() {
    // binary tree creation
    Node* root = getNode(1);
    root->left = getNode(2);
    root->right = getNode(3);
    root->left->left = getNode(4);
    root->left->right = getNode(5);
    root->right->left = getNode(6);
    root->right->right = getNode(7);
      cout << "Sum = "<< minLeafSum(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
#define bool int
 
/* 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;
};
 
/*
Given a tree and a sum, return true if there is a path from
the root down to a leaf, such that adding up all the values
along the path equals the given sum.
 
Strategy: subtract the node value from the sum when
recurring down, and check to see if the sum is 0 when you
when you reach the leaf node.
*/
bool hasPathSum(node* Node, int sum)
{
    if (Node == NULL)
        return 0;
 
    bool ans = 0;
 
    int subSum = sum - Node->data;
 
    /* If we reach a leaf node and sum becomes 0 then return
     * true*/
    if (subSum == 0 && Node->left == NULL
        && Node->right == NULL)
        return 1;
 
    /* otherwise check both subtrees */
    if (Node->left)
        ans = ans || hasPathSum(Node->left, subSum);
    if (Node->right)
        ans = ans || hasPathSum(Node->right, subSum);
 
    return ans;
}
 
/* UTILITY FUNCTIONS */
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newnode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
 
    return (Node);
}
 
// Driver's Code
int main()
{
 
    int sum = 21;
 
    /* Constructed binary tree is
                10
            / \
            8 2
        / \ /
        3 5 2
    */
    node* root = newnode(10);
    root->left = newnode(8);
    root->right = newnode(2);
    root->left->left = newnode(3);
    root->left->right = newnode(5);
    root->right->left = newnode(2);
     
      // Function call
    if (hasPathSum(root, sum))
        cout << "There is a root-to-leaf path with sum "
             << sum;
    else
        cout << "There is no root-to-leaf path with sum "
             << sum;
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: logn
// C++ program to find sum of all paths from root to leaves
 
// A Binary tree node
#include <bits/stdc++.h>
using namespace std;
 
// A Binary tree node
class Node
{
    public:
    int data;
    Node *left, *right;
   
    // Constructor to create a new node
      Node(int val)
    {
      data = val;
      left = right = NULL;
    }
};
 
void treePathsSumUtil(Node* root, vector<string> currPath,
                      vector<vector<string>> &allPath)
{
    // Base Case
    if (root == NULL)
        return;
 
    // append the root data in string format in currPath
    currPath.push_back((to_string)(root->data));
 
    // if we found a leaf node we copy the currPath to allPath
    if (root->left == NULL && root->right == NULL)
        allPath.push_back(currPath);
 
    // traverse in the left subtree
    treePathsSumUtil(root->left, currPath, allPath);
 
    // traverse in the right subtree
    treePathsSumUtil(root->right, currPath, allPath);
 
    // remove the current element from the path
    currPath.pop_back();
}
int treePathsSum(Node* root)
{
   
    // store all the root to leaf path in allPath
    vector<vector<string>> allPath;
    vector<string> v;
    treePathsSumUtil(root, v, allPath);
   
    // store the sum
    int s = 0;
 
    for(auto pathNumber : allPath)
    {
        string k="";
       
        // join the pathNumbers to convert them 
      // into the number to calculate sum
        for(auto x: pathNumber)
            k = k+x;
        s += stoi(k);
    }
    return s;
}
  
// Driver code
int main()
{
    Node *root = new Node(6);
    root->left = new Node(3);
    root->right = new Node(5);
    root->left->left = new Node(2);
    root->left->right = new Node(5);
    root->right->right = new Node(4);
    root->left->right->left = new Node(7);
    root->left->right->right = new Node(4);
    cout<<"Sum of all paths is "<<treePathsSum(root);
    return 0;
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: quadratic
Space complexity: linear
// Find root of tree where children
// sum for every node id is given.
#include<bits/stdc++.h>
using namespace std;
 
int findRoot(pair<int, int> arr[], int n)
{
   // Every node appears once as an id, and
   // every node except for the root appears
   // once in a sum.  So if we subtract all
   // the sums from all the ids, we're left
   // with the root id.
   int root = 0;
   for (int i=0; i<n; i++)
     root += (arr[i].first - arr[i].second);
 
   return root;
}
 
// Driver code
int main()
{
    pair<int, int> arr[] = {{1, 5}, {2, 0},
           {3, 0}, {4, 0}, {5, 5}, {6, 5}};
    int n = sizeof(arr)/sizeof(arr[0]);
    printf("%d\n", findRoot(arr, n));
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ Program for Lowest Common Ancestor
// in a Binary Tree
// A O(n) solution to find LCA
// of two given values n1 and n2
 
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Utility function creates a new binary tree node with
// given key
Node* newNode(int k)
{
    Node* temp = new Node;
    temp->key = k;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Finds the path from root node to given root of the tree,
// Stores the path in a vector path[], returns true if path
// exists otherwise false
bool findPath(Node* root, vector<int>& path, int k)
{
    // base case
    if (root == NULL)
        return false;
 
    // Store this node in path vector. The node will be
    // removed if not in path from root to k
    path.push_back(root->key);
 
    // See if the k is same as root's key
    if (root->key == k)
        return true;
 
    // Check if k is found in left or right sub-tree
    if ((root->left && findPath(root->left, path, k))
        || (root->right && findPath(root->right, path, k)))
        return true;
 
    // If not present in subtree rooted with root, remove
    // root from path[] and return false
    path.pop_back();
    return false;
}
 
// Returns LCA if node n1, n2 are present in the given
// binary tree, otherwise return -1
int findLCA(Node* root, int n1, int n2)
{
    // to store paths to n1 and n2 from the root
    vector<int> path1, path2;
 
    // Find paths from root to n1 and root to n2. If either
    // n1 or n2 is not present, return -1
    if (!findPath(root, path1, n1)
        || !findPath(root, path2, n2))
        return -1;
 
    /* Compare the paths to get the first different value */
    int i;
    for (i = 0; i < path1.size() && i < path2.size(); i++)
        if (path1[i] != path2[i])
            break;
    return path1[i - 1];
}
 
// Driver program to test above functions
int main()
{
    // Let us create the 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(6);
    root->right->right = newNode(7);
    cout << "LCA(4, 5) = " << findLCA(root, 4, 5);
    cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6);
    cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4);
    cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4);
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C++ Program to find LCA of n1 and n2 using one traversal
 * of Binary Tree */
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    struct Node *left, *right;
    int key;
};
 
// Utility function to create a new tree Node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// This function returns pointer to LCA of two given values
// n1 and n2. This function assumes that n1 and n2 are
// present in Binary Tree
struct Node* findLCA(struct Node* root, int n1, int n2)
{
    // Base case
    if (root == NULL)
        return NULL;
 
    // If either n1 or n2 matches with root's key, report
    // the presence by returning root (Note that if a key is
    // ancestor of other, then the ancestor key becomes LCA
    if (root->key == n1 || root->key == n2)
        return root;
 
    // Look for keys in left and right subtrees
    Node* left_lca = findLCA(root->left, n1, n2);
    Node* right_lca = findLCA(root->right, n1, n2);
 
    // If both of the above calls return Non-NULL, then one
    // key is present in once subtree and other is present
    // in other, So this node is the LCA
    if (left_lca && right_lca)
        return root;
 
    // Otherwise check if left subtree or right subtree is
    // LCA
    return (left_lca != NULL) ? left_lca : right_lca;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree given in the above example
    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(6);
    root->right->right = newNode(7);
    cout << "LCA(4, 5) = " << findLCA(root, 4, 5)->key;
    cout << "\nLCA(4, 6) = " << findLCA(root, 4, 6)->key;
    cout << "\nLCA(3, 4) = " << findLCA(root, 3, 4)->key;
    cout << "\nLCA(2, 4) = " << findLCA(root, 2, 4)->key;
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: logn
/* C++ program to find LCA of n1 and n2 using one traversal
   of Binary Tree. It handles all cases even when n1 or n2
   is not there in Binary Tree */
 
#include <iostream>
using namespace std;
 
// A Binary Tree Node
struct Node {
    struct Node *left, *right;
    int key;
};
 
// Utility function to create a new tree Node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
}
 
// This function returns pointer to LCA of two given
// valuesn1 and n2.
struct Node* findLCAUtil(struct Node* root, int n1, int n2)
{
    // Base case
    if (root == NULL)
        return NULL;
 
    // If either n1 or n2 matches with root's key, report
    // the presence by returning root
    if (root->key == n1 || root->key == n2)
        return root;
 
    // Look for keys in left and right subtrees
    Node* left_lca = findLCAUtil(root->left, n1, n2);
    Node* right_lca = findLCAUtil(root->right, n1, n2);
 
    // If both of the above calls return Non-NULL nodes,
    // then one key is present in once subtree and other is
    // present in other, So this node is the LCA
    if (left_lca and right_lca)
        return root;
 
    // Otherwise check if left subtree or right subtree is
    // LCA
    return (left_lca != NULL) ? left_lca : right_lca;
}
 
// Returns true if key k is present in tree rooted with root
bool find(Node* root, int k)
{
    // Base Case
    if (root == NULL)
        return false;
 
    // If key is present at root, or in left subtree or
    // right subtree, return true;
    if (root->key == k || find(root->left, k)
        || find(root->right, k))
        return true;
 
    // Else return false
    return false;
}
 
// This function returns LCA of n1 and n2 only if both n1
// and n2 are present in tree, otherwise returns NULL;
Node* findLCA(Node* root, int n1, int n2)
{
    // Return LCA only if both n1 and n2 are present in tree
    if (find(root, n1) and find(root, n2))
        return findLCAUtil(root, n1, n2);
 
    // Else return NULL
    return NULL;
}
 
// Driver program to test above functions
int main()
{
    // Let us create a binary tree given in the above
    // example
    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(6);
    root->right->right = newNode(7);
 
    Node* lca = findLCA(root, 4, 5);
 
    if (lca != NULL)
        cout << "LCA(4, 5) = " << lca->key;
    else
        cout << "Keys are not present ";
 
    lca = findLCA(root, 4, 10);
 
    if (lca != NULL)
        cout << "\nLCA(4, 10) = " << lca->key;
    else
        cout << "\nKeys are not present ";
 
    return 0;
}
 
// This code is contributed by Kshitij Dwivedi
// (kshitijdwivedi28)

Time complexity: linear
Space complexity: logn
// C++ program to find maximum difference between node
// and its ancestor
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, pointer to left
   child and a pointer to right child */
struct Node {
    int key;
    struct Node *left, *right;
};
 
/* To create a newNode of tree and return pointer */
struct Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
 
/* Recursive function to calculate maximum ancestor-node
   difference in  binary tree. It updates value at 'res'
   to store the result.  The returned value of this function
   is minimum value in subtree rooted with 't' */
int maxDiffUtil(Node* t, int* res)
{
    /* Returning Maximum int value if node is not
       there (one child case)  */
    if (t == NULL)
        return INT_MAX;
 
    /* If leaf node then just return node's value  */
    if (t->left == NULL && t->right == NULL)
        return t->key;
 
    /* Recursively calling left and right subtree
       for minimum value  */
    int val = min(maxDiffUtil(t->left, res),
                  maxDiffUtil(t->right, res));
 
    /* Updating res if (node value - minimum value
       from subtree) is bigger than res  */
    *res = max(*res, t->key - val);
 
    /* Returning minimum value got so far */
    return min(val, t->key);
}
 
/* This function mainly calls maxDiffUtil() */
int maxDiff(Node* root)
{
    // Initialising result with minimum int value
    int res = INT_MIN;
 
    maxDiffUtil(root, &res);
 
    return res;
}
 
/* Helper function to print inorder traversal of
  binary tree   */
void inorder(Node* root)
{
    if (root) {
        inorder(root->left);
        cout << root->key << " ";
        inorder(root->right);
    }
}
 
// Driver program to test above functions
int main()
{
    // Making above given diagram's binary tree
    Node* root;
    root = newNode(8);
    root->left = newNode(3);
 
    root->left->left = newNode(1);
    root->left->right = newNode(6);
    root->left->right->left = newNode(4);
    root->left->right->right = newNode(7);
 
    root->right = newNode(10);
    root->right->right = newNode(14);
    root->right->right->left = newNode(13);
 
    cout << maxDiff(root);
}

Time complexity: linear
Space complexity: linear
// C++ program to count number of ways to color
// a N node skewed tree with k colors such that
// parent and children have different colors.
#include <bits/stdc++.h>
using namespace std;
 
// fast_way is recursive
// method to calculate power
int fastPow(int N, int K)
{
    if (K == 0)
        return 1;
    int temp = fastPow(N, K / 2);
    if (K % 2 == 0)
        return temp * temp;
    else
        return N * temp * temp;
}
 
int countWays(int N, int K)
{
    return K * fastPow(K - 1, N - 1);
}
 
// driver program
int main()
{
    int N = 3, K = 3;
    cout << countWays(N, K);
    return 0;
}

Time complexity: logn
Space complexity: constant
// C++ program to print size of tree in iterative
#include<iostream>
#include<queue>
using namespace std;
  
struct Node 
{
    int data;
    Node *left, *right;
};
  
// A utility function to
// create a new Binary Tree Node
Node *newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
      
    return temp;
}
  
// return size of tree
int sizeoftree(Node *root)
{
  
    // if tree is empty it will
    // return 0
    if(root == NULL)
        return 0;
      
      
    // Using level order Traversal.
    queue<Node *> q;
    int count = 1;
    q.push(root);
      
    while(!q.empty())
    {
        Node *temp = q.front();
      
        if(temp->left)
        {
            // Enqueue left child 
            q.push(temp->left);
              
            // Increment count
            count++;
        }
      
        if(temp->right)
        {
            // Enqueue right child 
            q.push(temp->right);
              
            // Increment count
            count++;
        }
        q.pop();
    }
      
    return count;
}
  
// Driver Code
int main()
{
    Node *root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
  
    cout << "Size of the tree is " <<
        sizeoftree(root) << endl;
    return 0;
}
  
// This code is contributed by SHAKEELMOHAMMAD

Time complexity: linear
Space complexity: np
// C++ program to find height of 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;
};
  
/* Compute the "maxDepth" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
int maxDepth(node* node)
{
    if (node == NULL)
        return 0;
    else {
        /* compute the depth of each subtree */
        int lDepth = maxDepth(node->left);
        int rDepth = maxDepth(node->right);
  
        /* use the larger one */
        if (lDepth > rDepth)
            return (lDepth + 1);
        else
            return (rDepth + 1);
    }
}
  
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
  
    return (Node);
}
  
// Driver code
int main()
{
    node* root = newNode(1);
  
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
  
    cout << "Height of tree is " << maxDepth(root);
    return 0;
}
  
// This code is contributed by Amit Srivastav

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
#include <iostream>
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(struct Node* root)
{
  
    // Initialising a variable to count the
    // height of tree
    int depth = 0;
  
    queue<Node*> q;
  
    // Pushing first level element along with NULL
    q.push(root);
    q.push(NULL);
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
  
        // When NULL encountered, increment the value
        if (temp == NULL) {
            depth++;
        }
  
        // If NULL not encountered, keep moving
        if (temp != NULL) {
            if (temp->left) {
                q.push(temp->left);
            }
            if (temp->right) {
                q.push(temp->right);
            }
        }
  
        // If queue still have elements left,
        // push NULL again to the queue.
        else if (!q.empty()) {
            q.push(NULL);
        }
    }
    return depth;
}
  
// 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);
}

Time complexity: linear
Space complexity: linear