Banana-Leopard/TASTY huggingface dataset:
// Iterative C++ program to print odd level nodes
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    Node* left, *right;
};
 
// Iterative method to do level order traversal line by line
void printOddNodes(Node *root)
{
    // Base Case
    if (root == NULL)  return;
 
    // Create an empty queue for level
    // order traversal
    queue<Node *> q;
 
    // Enqueue root and initialize level as odd
    q.push(root);
    bool isOdd = true; 
 
    while (1)
    {
        // nodeCount (queue size) indicates
        // number of nodes at current level.
        int nodeCount = q.size();
        if (nodeCount == 0)
            break;
 
        // Dequeue all nodes of current level
        // and Enqueue all nodes of next level
        while (nodeCount > 0)
        {
            Node *node = q.front();
            if (isOdd)
               cout << node->data << " ";
            q.pop();
            if (node->left != NULL)
                q.push(node->left);
            if (node->right != NULL)
                q.push(node->right);
            nodeCount--;
        }
 
        isOdd = !isOdd;
    }
}
 
// Utility method to create a node
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);
    printOddNodes(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// A C++ program to find the all full nodes in
// a given binary tree
#include <bits/stdc++.h>
using namespace std;
 
struct Node
{
    int data;
    struct Node *left, *right;
};
 
Node *newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Traverses given tree in Inorder fashion and
// prints all nodes that have both children as
// non-empty.
void findFullNode(Node *root)
{
    if (root != NULL)
    {
        findFullNode(root->left);
        if (root->left != NULL && root->right != NULL)
            cout << root->data << " ";
        findFullNode(root->right);
    }
}
 
// Driver program to test above function
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);
    findFullNode(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
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 sum of all elements*/
int sumBT(Node* root)
{
      //sum variable to track the sum of
      //all variables.
    int sum = 0;
   
    queue<Node*> q;
 
      //Pushing the first level.
    q.push(root);
 
      //Pushing elements at each level from
      //the tree.
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
       
          //After popping each element from queue
          //add its data to the sum variable.
        sum += temp->key;
 
        if (temp->left) {
            q.push(temp->left);
        }
        if (temp->right) {
            q.push(temp->right);
        }
    }
    return sum;
}
 
// 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);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->right->left->right = newNode(8);
 
    cout << "Sum of all elements in the binary tree is: "
         << sumBT(root);
}
 
//This code is contributed by Sarthak Delori

Time complexity: linear
Space complexity: linear
// C++ implementation to find the sum of all
// the parent nodes having child node x
#include <bits/stdc++.h>
 
using namespace std;
 
// Node of a binary tree
struct Node
{
    int data;
    Node *left, *right;
};
 
// function to get a new node
Node* getNode(int data)
{
    // allocate memory for the node
    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 all the
// parent nodes having child node x
void sumOfParentOfX(Node* root, int& sum, int x)
{
    // if root == NULL
    if (!root)
        return;
     
    // if left or right child of root is 'x', then
    // add the root's data to 'sum'
    if ((root->left && root->left->data == x) ||
        (root->right && root->right->data == x))
        sum += root->data;
     
    // recursively find the required parent nodes
    // in the left and right subtree   
    sumOfParentOfX(root->left, sum, x);
    sumOfParentOfX(root->right, sum, x);
     
}
 
// utility function to find the sum of all
// the parent nodes having child node x
int sumOfParentOfXUtil(Node* root, int x)
{
    int sum = 0;
    sumOfParentOfX(root, sum, x);
     
    // required sum of parent nodes
    return sum;
}
 
// Driver program to test above
int main()
{
    // binary tree formation
    Node *root = getNode(4);           /*        4        */
    root->left = getNode(2);           /*       / \       */
    root->right = getNode(5);          /*      2   5      */
    root->left->left = getNode(7);     /*     / \ / \     */
    root->left->right = getNode(2);    /*    7  2 2  3    */
    root->right->left = getNode(2);
    root->right->right = getNode(3);
     
    int x = 2;
     
    cout << "Sum = "
         << sumOfParentOfXUtil(root, x);
          
    return 0;   
}

Time complexity: linear
Space complexity: linear
// CPP program to find total sum
// of right leaf nodes
#include <bits/stdc++.h>
using namespace std;
 
// struct node of binary tree
struct Node {
    int data;
    Node *left, *right;
};
 
// return new node
Node* addNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// utility function to calculate sum
// of right leaf nodes
void rightLeafSum(Node* root, int& sum)
{
    if (!root)
        return;
 
    // check if the right child of root
    // is leaf node
    if (root->right)
        if (root->right->left == NULL
            && root->right->right == NULL)
            sum += root->right->data;
 
    rightLeafSum(root->left, sum);
    rightLeafSum(root->right, sum);
}
 
// driver program
int main()
{
 
    // construct binary tree
    Node* root = addNode(1);
    root->left = addNode(2);
    root->left->left = addNode(4);
    root->left->right = addNode(5);
    root->left->left->right = addNode(2);
    root->right = addNode(3);
    root->right->right = addNode(8);
    root->right->right->left = addNode(6);
    root->right->right->right = addNode(7);
 
    // variable to store sum of right
    // leaves
    int sum = 0;
    rightLeafSum(root, sum);
    cout << sum << endl;
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to find total sum of right leaf nodes.
#include <bits/stdc++.h>
using namespace std;
 
// struct node of Binary Tree
struct Node {
    int data;
    Node *left, *right;
};
 
// fun to create and return a new node
Node* addNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// fun to calculate sum of right leaf nodes
int rightLeafSum(Node* root, bool isrightleaf)
{
    // base case
    if (!root)
        return 0;
 
    // if it is a leaf node and right node, the return
    // root's data.
    if (!root->left && !root->right && isrightleaf)
        return root->data;
 
    // recur of left subtree and right subtree and do the
    // summation simultaniously.
    return rightLeafSum(root->left, false)
           + rightLeafSum(root->right, true);
}
 
int main()
{
    // create a tree
    Node* root = addNode(1);
    root->left = addNode(2);
    root->left->left = addNode(4);
    root->left->right = addNode(5);
    root->left->left->right = addNode(2);
    root->right = addNode(3);
    root->right->right = addNode(8);
    root->right->right->left = addNode(6);
    root->right->right->right = addNode(7);
 
    cout << rightLeafSum(root, false);
 
    return 0;
}
// This code is contributed by Modem Upendra

Time complexity: linear
Space complexity: logn
// CPP program to find total sum
// of right leaf nodes
#include <bits/stdc++.h>
using namespace std;
 
// struct node of binary tree
struct Node {
    int data;
    Node *left, *right;
};
 
// return new node
Node* addNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// utility function to calculate sum
// of right leaf nodes iteratively
int rightLeafSum(Node* root)
{
    // declaring sum to store sum of right leaves
    int sum = 0;
 
    // queue of Node* type
    queue<Node*> q;
    q.push(root);
    while (!q.empty()) {
        Node* curr = q.front();
        q.pop();
        // check for left node
        if (curr->left) {
            q.push(curr->left);
        }
        // check for right node
        if (curr->right) {
 
            // check for right leaf node
            if (curr->right->right == NULL
                and curr->right->left == NULL) {
                // incrementing sum for found right leaf
                // node
                sum += curr->right->data;
            }
            q.push(curr->right);
        }
    }
    return sum;
}
 
// driver program
int main()
{
 
    // construct binary tree
    Node* root = addNode(1);
    root->left = addNode(2);
    root->left->left = addNode(4);
    root->left->right = addNode(5);
    root->left->left->right = addNode(2);
    root->right = addNode(3);
    root->right->right = addNode(8);
    root->right->right->left = addNode(6);
    root->right->right->right = addNode(7);
 
    int sum = rightLeafSum(root);
    cout << sum << endl;
    return 0;
}

Time complexity: linear
Space complexity: np
#include <bits/stdc++.h>
using namespace std;
 
//Building a tree node having left and right pointers set to null initially
struct Node
{
  Node* left;
  Node* right;
  int data;
  //constructor to set the data of the newly created tree node
  Node(int element){
     data = element;
     this->left = nullptr;
     this->right = nullptr;
  }
};
 
int longestPathLeaf(Node* root){
   
  /* structure to store current Node,it's level and sum in the path*/
  struct Element{
    Node* data;
    int level;
    int sum;
  };
   
  /*
    maxSumLevel stores maximum sum so far in the path
    maxLevel stores maximum level so far
  */
  int maxSumLevel = root->data,maxLevel = 0;
 
  /* queue to implement level order traversal */
   
  list<Element> que;
  Element e;
   
  /* Each element variable stores the current Node, it's level, sum in the path */
 
  e.data = root;
  e.level = 0;
  e.sum = root->data;
   
  /* push the root element*/
  que.push_back(e);
   
  /* do level order traversal on the tree*/
  while(!que.empty()){
 
     Element front = que.front();
     Node* curr = front.data;
     que.pop_front();
      
     /* if the level of current front element is greater than the maxLevel so far then update maxSum*/
     if(front.level > maxLevel){
        maxSumLevel = front.sum;
        maxLevel = front.level;
     }
     /* if another path competes then update if the sum is greater than the previous path of same height*/
     else if(front.level == maxLevel && front.sum > maxSumLevel)
        maxSumLevel = front.sum;
 
     /* push the left element if exists*/ 
     if(curr->left){
        e.data = curr->left;
        e.sum = e.data->data;
        e.sum +=  front.sum;
        e.level = front.level+1;
        que.push_back(e);
     }
     /*push the right element if exists*/
     if(curr->right){
        e.data = curr->right;
        e.sum = e.data->data;
        e.sum +=  front.sum;
        e.level = front.level+1;
        que.push_back(e);
     }
  }
 
  /*return the answer*/
  return maxSumLevel;
}
//Helper function
int main() {
   
  Node* root = new Node(4);        
  root->left = new Node(2);        
  root->right = new Node(5);       
  root->left->left = new Node(7);  
  root->left->right = new Node(1); 
  root->right->left = new Node(2);
  root->right->right = new Node(3);
  root->left->right->left = new Node(6);
   
  cout << longestPathLeaf(root) << "\n";
     
  return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find maximum path
//sum between two leaves of  a binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A binary tree node
struct Node
{
    int data;
    struct Node* left, *right;
};
 
// Utility function to allocate memory for a new node
struct Node* newNode(int data)
{
    struct Node* node = new(struct Node);
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
// Utility function to find maximum of two integers
int max(int a, int b)
{ return (a >= b)? a: b; }
 
// A utility function to find the maximum sum between any
// two leaves.This function calculates two values:
// 1) Maximum path sum between two leaves which is stored
//    in res.
// 2) The maximum root to leaf path sum which is returned.
// If one side of root is empty, then it returns INT_MIN
int maxPathSumUtil(struct Node *root, int &res)
{
    // Base cases
    if (root==NULL) return 0;
    if (!root->left && !root->right) return root->data;
 
    // Find maximum sum in left and right subtree. Also
    // find maximum root to leaf sums in left and right
    // subtrees and store them in ls and rs
    int ls = maxPathSumUtil(root->left, res);
    int rs = maxPathSumUtil(root->right, res);
 
 
    // If both left and right children exist
    if (root->left && root->right)
    {
        // Update result if needed
        res = max(res, ls + rs + root->data);
 
        // Return maximum possible value for root being
        // on one side
        return max(ls, rs) + root->data;
    }
 
    // If any of the two children is empty, return
    // root sum for root being on one side
    return (!root->left)? rs + root->data:
                          ls + root->data;
}
 
// The main function which returns sum of the maximum
// sum path between two leaves. This function mainly
// uses maxPathSumUtil()
int maxPathSum(struct Node *root)
{
    int res = INT_MIN;
   
    int val = maxPathSumUtil(root, res);
           
      //--- for test case ---
       //         7                
      //        /    \              
        //    Null   -3           
      //     (case - 1)        
      //   value of res will be INT_MIN but the answer is 4 , which is returned by the
      // function maxPathSumUtil().
   
      if(root->left && root->right)
            return res;
        return max(res, val);
}
 
// Driver Code
int main()
{
    struct Node *root = newNode(-15);
    root->left = newNode(5);
    root->right = newNode(6);
    root->left->left = newNode(-8);
    root->left->right = newNode(1);
    root->left->left->left = newNode(2);
    root->left->left->right = newNode(6);
    root->right->left = newNode(3);
    root->right->right = newNode(9);
    root->right->right->right= newNode(0);
    root->right->right->right->left= newNode(4);
    root->right->right->right->right= newNode(-1);
    root->right->right->right->right->left= newNode(10);
    cout << "Max pathSum of the given binary tree is "
         << maxPathSum(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print all paths with sum k.
#include <bits/stdc++.h>
using namespace std;
  
// utility function to print contents of
// a vector from index i to it's end
void printVector(const vector<int>& v, int i)
{
    for (int j = i; j < v.size(); j++)
        cout << v[j] << " ";
    cout << endl;
}
  
// binary tree node
struct Node {
    int data;
    Node *left, *right;
    Node(int x)
    {
        data = x;
        left = right = NULL;
    }
};
  
// This function prints all paths that have sum k
void printKPathUtil(Node* root, vector<int>& path, int k)
{
    // empty node
    if (!root)
        return;
  
    // add current node to the path
    path.push_back(root->data);
  
    // check if there's any k sum path
    // in the left sub-tree.
    printKPathUtil(root->left, path, k);
  
    // check if there's any k sum path
    // in the right sub-tree.
    printKPathUtil(root->right, path, k);
  
    // check if there's any k sum path that
    // terminates at this node
    // Traverse the entire path as
    // there can be negative elements too
    int f = 0;
    for (int j = path.size() - 1; j >= 0; j--) {
        f += path[j];
  
        // If path sum is k, print the path
        if (f == k)
            printVector(path, j);
    }
  
    // Remove the current element from the path
    path.pop_back();
}
  
// A wrapper over printKPathUtil()
void printKPath(Node* root, int k)
{
    vector<int> path;
    printKPathUtil(root, path, k);
}
  
// Driver code
int main()
{
    Node* root = new Node(1);
    root->left = new Node(3);
    root->left->left = new Node(2);
    root->left->right = new Node(1);
    root->left->right->left = new Node(1);
    root->right = new Node(-1);
    root->right->left = new Node(4);
    root->right->left->left = new Node(1);
    root->right->left->right = new Node(2);
    root->right->right = new Node(5);
    root->right->right->right = new Node(2);
  
    int k = 5;
    printKPath(root, k);
  
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to find if there is a subtree with
// given sum
#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;
};
 
/* utility 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);
}
 
// function to check if there exist any subtree with given sum
// cur_sum  --> sum of current subtree from ptr as root
// sum_left --> sum of left subtree from ptr as root
// sum_right --> sum of right subtree from ptr as root
bool sumSubtreeUtil(struct Node *ptr, int *cur_sum, int sum)
{
    // base condition
    if (ptr == NULL)
    {
        *cur_sum = 0;
        return false;
    }
 
    // Here first we go to left sub-tree, then right subtree
    // then first we calculate sum of all nodes of subtree
    // having ptr as root and assign it as cur_sum
    // cur_sum = sum_left + sum_right + ptr->data
    // after that we check if cur_sum == sum
    int sum_left = 0, sum_right = 0;
    return ( sumSubtreeUtil(ptr->left, ∑_left, sum) ||
             sumSubtreeUtil(ptr->right, ∑_right, sum) ||
        ((*cur_sum = sum_left + sum_right + ptr->data) == sum));
}
 
// Wrapper over sumSubtreeUtil()
bool sumSubtree(struct Node *root, int sum)
{
    // Initialize sum of subtree with root
    int cur_sum = 0;
 
    return sumSubtreeUtil(root, &cur_sum, sum);
}
 
// driver program to run the case
int main()
{
    struct Node *root = newnode(8);
    root->left    = newnode(5);
    root->right   = newnode(4);
    root->left->left = newnode(9);
    root->left->right = newnode(7);
    root->left->right->left = newnode(1);
    root->left->right->right = newnode(12);
    root->left->right->right->right = newnode(2);
    root->right->right = newnode(11);
    root->right->right->left = newnode(3);
    int sum = 22;
 
    if (sumSubtree(root, sum))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ implementation to count subtrees that
// sum up to a given value x
#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 count subtrees that
// sum up to a given value x
int countSubtreesWithSumX(Node* root, int& count, int x)
{
    // if tree is empty
    if (!root)
        return 0;
 
    // sum of nodes in the left subtree
    int ls = countSubtreesWithSumX(root->left, count, x);
 
    // sum of nodes in the right subtree
    int rs = countSubtreesWithSumX(root->right, count, x);
 
    // sum of nodes in the subtree rooted
    // with 'root->data'
    int sum = ls + rs + root->data;
 
    // if true
    if (sum == x)
        count++;
 
    // return subtree's nodes sum
    return sum;
}
 
// utility function to count subtrees that
// sum up to a given value x
int countSubtreesWithSumXUtil(Node* root, int x)
{
    // if tree is empty
    if (!root)
        return 0;
 
    int count = 0;
 
    // sum of nodes in the left subtree
    int ls = countSubtreesWithSumX(root->left, count, x);
 
    // sum of nodes in the right subtree
    int rs = countSubtreesWithSumX(root->right, count, x);
 
    // if tree's nodes sum == x
    if ((ls + rs + root->data) == x)
        count++;
 
    // required count of subtrees
    return count;
}
 
// Driver program to test above
int main()
{
    /* binary tree creation
                5
              /   \
           -10     3
           /  \   /  \
          9    8 -4   7
    */
    Node* root = getNode(5);
    root->left = getNode(-10);
    root->right = getNode(3);
    root->left->left = getNode(9);
    root->left->right = getNode(8);
    root->right->left = getNode(-4);
    root->right->right = getNode(7);
 
    int x = 7;
 
    cout << "Count = "
         << countSubtreesWithSumXUtil(root, x);
 
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to find if
// there is a subtree with
// given sum
#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;
}
 
// Utility function to count subtrees that
// sum up to a given value x
int countSubtreesWithSumXUtil(Node* root, int x)
{
    static int count = 0;
    static Node* ptr = root;
    int l = 0, r = 0;
    if (root == NULL)
        return 0;
 
    l += countSubtreesWithSumXUtil(root->left, x);
 
    r += countSubtreesWithSumXUtil(root->right, x);
 
    if (l + r + root->data == x)
        count++;
 
    if (ptr != root)
        return l + root->data + r;
 
    return count;
}
 
// Driver code
int main()
{
    /* binary tree creation
              5
            /  \
          -10   3
          / \  / \
          9 8 -4 7
    */
    Node* root = getNode(5);
    root->left = getNode(-10);
    root->right = getNode(3);
    root->left->left = getNode(9);
    root->left->right = getNode(8);
    root->right->left = getNode(-4);
    root->right->right = getNode(7);
 
    int x = 7;
 
    cout << "Count = "
         << countSubtreesWithSumXUtil(root, x);
 
    return 0;
}
// This code is contributed by Sadik Ali

Time complexity: linear
Space complexity: logn