code
stringlengths
195
7.9k
space_complexity
stringclasses
6 values
time_complexity
stringclasses
7 values
// C++ Program to convert a Binary Tree // to a Circular Doubly Linked List #include <iostream> using namespace std; // To represents a node of a Binary Tree struct Node { struct Node *left, *right; int data; }; // A function that appends rightList at the end // of leftList. Node* concatenate(Node* leftList...
logn
linear
// A C++ program for in-place conversion of Binary Tree to // CDLL #include <iostream> using namespace std; /* A binary tree node has - data , left and right pointers */ struct Node { int data; Node* left; Node* right; }; // A utility function that converts given binary tree to // a doubly linked list //...
logn
linear
// C++ program to convert a ternary expression to // a tree. #include<bits/stdc++.h> using namespace std; // tree structure struct Node { char data; Node *left, *right; }; // function create a new node Node *newNode(char Data) { Node *new_node = new Node; new_node->data = Data; new_node->left ...
linear
linear
// C++ program for Minimum swap required // to convert binary tree to binary search tree #include<bits/stdc++.h> using namespace std; // Inorder Traversal of Binary Tree void inorder(int a[], std::vector<int> &v, int n, int index) { // if index is greater or equal to vector size if(ind...
linear
nlogn
/* Program to check children sum property */ #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, left child and right child */ struct node { int data; struct node* left; struct node* right; }; /* returns 1 if children sum property holds for the given node and both of its childr...
logn
linear
// C++ program to check if two Nodes in a binary tree are // cousins #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // A utility function to create a new Binary Tree Node struct Node* newNode(int item) { struct Node* temp ...
linear
linear
// C++ program to check if all leaves // are at same level #include <bits/stdc++.h> using namespace std; // A binary tree node struct Node { int data; struct Node *left, *right; }; // A utility function to allocate // a new tree node struct Node* newNode(int data) { struct Node* node = (struct Node*) ma...
linear
linear
// C++ program to check if all leaf nodes are at // same level 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 = ...
linear
linear
// C++ program to check if there exist an edge whose // removal creates two trees of same size #include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* left, *right; }; // utility function to create a new node struct Node* newNode(int x) { struct Node* temp = new Node; temp-...
linear
quadratic
// C++ program to check if there exist an edge whose // removal creates two trees of same size #include<bits/stdc++.h> using namespace std; struct Node { int data; struct Node* left, *right; }; // utility function to create a new node struct Node* newNode(int x) { struct Node* temp = new Node; temp-...
linear
linear
/* C++ program to check if all three given traversals are of the same tree */ #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // Utility function to create a new tree node Node* newNode(int data) { Node *temp = new Node; te...
linear
quadratic
#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; Node(int val) { data = val; left = right = NULL; } }; Node* buildTreeFromInorderPreorder( int inStart, int inEnd, int& preIndex, int preorder[], unordered_map<int, int>& inorderIndexMa...
linear
linear
// C++ code to check if leaf traversals // of two Binary Trees are same or not. #include <bits/stdc++.h> using namespace std; // Binary Tree Node struct Node { int data; Node* left; Node* right; }; // Returns new Node with data as // input to below function. Node* newNode(int d) { Node* temp = new N...
nlogn
linear
// C++ program to check whether a given Binary Tree is full or not #include <bits/stdc++.h> using namespace std; /* Tree node structure */ struct Node { int key; struct Node *left, *right; }; /* Helper function that allocates a new node with the given key and NULL left and right pointer. */ struct N...
linear
linear
// c++ program to check whether a given BT is full or not #include <bits/stdc++.h> using namespace std; // Tree node structure struct Node { int val; Node *left, *right; }; // fun that creates and returns a new node Node* newNode(int data) { Node* node = new Node(); node->val = data; node->left ...
linear
linear
// C++ implementation to check whether a binary // tree is a full binary tree or not #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* newN...
constant
linear
// C++ program to check if a given binary tree is complete // or not #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; }; // Given a binary tree, return true...
linear
linear
/* Program to check if a given Binary Tree is balanced like a Red-Black Tree */ #include <bits/stdc++.h> using namespace std; struct Node { int key; Node *left, *right; }; /* utility that allocates a new Node with the given key */ Node* newNode(int key) { Node* node = new Node; node->key = key; ...
linear
linear
// C++ program to check if two trees are mirror // of each other #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; Node* left, *right; }; /* Given two trees, return true if they are mirror of each ...
logn
linear
// C++ implementation to check whether the two // binary trees are mirrors of each other or not #include <bits/stdc++.h> using namespace std; // structure of a node in binary tree struct Node { int data; struct Node *left, *right; }; // Utility function to create and return // a new node for a binary tr...
linear
linear
// C++ program to check if a given Binary // Tree is symmetric or not #include<bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int key; struct Node* left, *right; }; // Utility function to create new Node Node *newNode(int key) { Node *temp = new Node; temp->key = key; te...
logn
linear
#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-...
logn
linear
// C++ program to Print root to leaf path WITHOUT // using recursion #include <bits/stdc++.h> using namespace std; /* A binary tree */ struct Node { int data; struct Node *left, *right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers.*/ Node* newNod...
linear
nlogn
// C++ program to print root to leaf path without using // recursion #include <bits/stdc++.h> using namespace std; // A binary tree node structure struct Node { int data; Node *left, *right; }; // fun to create a new node Node* newNode(int data) { Node* temp = new Node(); temp->data = data; temp...
linear
linear
// C++ program to print all root to leaf paths // with there relative position #include<bits/stdc++.h> using namespace std; #define MAX_PATH_SIZE 1000 // tree structure struct Node { char data; Node *left, *right; }; // function create new node Node * newNode(char data) { struct Node *temp = new Node;...
logn
constant
// Recursive C++ program to print odd level nodes #include <bits/stdc++.h> using namespace std; struct Node { int data; Node* left, *right; }; void printOddNodes(Node *root, bool isOdd = true) { // If empty tree if (root == NULL) return; // If current node is of odd level if (isOdd) ...
linear
linear
// 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 empt...
linear
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; } // T...
linear
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 f...
linear
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 *n...
linear
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 = N...
logn
linear
// 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...
logn
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 = N...
np
linear
#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...
linear
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* nod...
linear
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; } // binar...
logn
linear
// 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 ...
logn
linear
// 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 = (No...
logn
linear
// 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*)mall...
logn
linear
// C++ implementation to find maximum spiral sum #include <bits/stdc++.h> using namespace std; // structure of a node of binary tree struct Node { int data; Node *left, *right; }; // A utility function to create a new node Node* newNode(int data) { // allocate space Node* node = new Node; //...
linear
linear
// 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*)...
linear
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 = ...
linear
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 t...
logn
linear
// 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 = ri...
linear
quadratic
// 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 t...
constant
linear
// C++ implementation to replace each node // in binary tree with the sum of its inorder // predecessor and successor #include <bits/stdc++.h> using namespace std; // node of a binary tree struct Node { int data; struct Node* left, *right; }; // function to get a new node of a binary tree struct Node* get...
linear
linear
// C++ implementation to replace each node // in binary tree with the sum of its inorder // predecessor and successor #include <bits/stdc++.h> using namespace std; // node of a binary tree struct Node { int data; struct Node* left, *right; }; // function to get a new node of a binary tree struct Node* ...
linear
linear
// 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 // ...
linear
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...
logn
linear
/* 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 t...
logn
linear
// 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 poin...
linear
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 =...
constant
logn
// 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 = N...
np
linear
// 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 ...
linear
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); } ...
linear
linear
// 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); ...
linear
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...
linear
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; }
constant
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(No...
linear
linear
/* CPP program to check if a tree is height-balanced or not */ #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) { int data = d; l...
linear
quadratic
// 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 ret...
linear
quadratic
// 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 r...
linear
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) retu...
logn
linear
// 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...
linear
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;...
linear
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; rig...
linear
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 = NU...
linear
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->righ...
constant
quadratic
// 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->d...
np
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 = th...
logn
linear
// 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) ...
logn
linear
// 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) { // Cod...
linear
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 se...
linear
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; te...
linear
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; ...
linear
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) { ...
linear
nlogn
// 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; // C...
linear
nlogn
#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...
linear
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 unsig...
linear
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 unsig...
linear
linear
// C++ program to count // full nodes in a Binary Tree #include <bits/stdc++.h> using namespace std; // A binary tree Node has data, pointer to left // child and a pointer to right child struct Node { int data; struct Node* left, *right; }; // Function to get the count of full Nodes in // a binary tree ...
linear
linear
// C++ program to count full nodes in a Binary Tree #include <bits/stdc++.h> using namespace std; // A binary tree Node has data, pointer to left // child and a pointer to right child struct Node { int data; struct Node* left, *right; }; // Function to get the count of full Nodes in // a binary tree uns...
linear
linear
// Connect nodes at same level using level order // traversal. #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* left, *right, *nextRight; }; // Sets nextRight of all nodes of a tree void connect(struct Node* root) { queue<Node*> q; q.push(root); // null marker...
linear
linear
// Iterative CPP program to connect // nodes at same level using // constant extra space #include<bits/stdc++.h> #include<bits/stdc++.h> using namespace std; class node { public: int data; node* left; node* right; node *nextRight; /* Constructor that allocates a new node with the giv...
constant
linear
/* Iterative program to connect all the adjacent nodes at * the same level in a binary tree*/ #include <iostream> #include <queue> using namespace std; // A Binary Tree Node class node { public: int data; node* left; node* right; node* nextRight; /* Constructor that allocates a new node with th...
linear
linear
// C++ implementation to find the level // having maximum number of Nodes #include <bits/stdc++.h> using namespace std; /* A binary tree Node has data, pointer to left child and a pointer to right child */ struct Node { int data; struct Node* left; struct Node* right; }; /* Helper function that ...
linear
linear
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // A binary tree node has data, pointer to // left child and a pointer to right child struct Node { int val; struct Node *left, *right; }; // Initialising a map with key as levels of the tree map<int, pair<double, double> > m...
logn
nlogn
// C++ implementation to print largest // value in each level of Binary Tree #include <bits/stdc++.h> using namespace std; // structure of a node of binary tree struct Node { int data; Node *left, *right; }; // function to get a new node Node* newNode(int data) { // allocate space Node* temp = new...
constant
linear
// C++ program to Get Level of a node in a Binary Tree #include <bits/stdc++.h> using namespace std; /* A tree node structure */ struct node { int data; struct node* left; struct node* right; }; // Helper function for getLevel(). It returns level of the // data if data is present in tree, otherwise return...
linear
linear
// C++ program to print level in which X is present in // binary tree using STL #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; int printLevel(Node* root, int X) { Node* node; // Base Case if (root == NULL) return ...
linear
linear
// CPP program to print level of given node // in binary tree iterative approach /* Example binary tree root is at level 1 20 / \ 10 30 / \ / \ 5 15 25 40 / 12 */ #include <bits/stdc++.h> using namespace std; // node...
linear
linear
// C++ program to remove all half nodes #include <bits/stdc++.h> using namespace std; struct node { int data; struct node* left, *right; }; struct node* newNode(int data) { node* nod = new node(); nod->data = data; nod->left = nod->right = NULL; return(nod); } void printInoder(struct node*...
constant
linear
/* C++ program to pairwise swap leaf nodes from left to right */ #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // function to swap two Node void Swap(Node **a, Node **b) { Node * temp = *a; *a = *b; *b = temp; } // two...
linear
linear
// C++ program to find the length of longest // path with same values 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 val; struct Node *left, *right; }; /* Function to print the longest path ...
logn
linear
// C++ program to find distance of a given // node from root. #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; Node *left, *right; }; // A utility function to create a new Binary // Tree Node Node *newNode(int item) { Node *temp = new Node; temp->data = ite...
constant
linear
// C program to print right sibling of a node #include <bits/stdc++.h> // A Binary Tree Node struct Node { int data; Node *left, *right, *parent; }; // A utility function to create a new Binary // Tree Node Node* newNode(int item, Node* parent) { Node* temp = new Node; temp->data = item; temp->l...
constant
linear
/* C++ program to find next right of a given key using preorder traversal */ #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-...
constant
linear
// CPP Program to find Tilt 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 */ struct Node { int val; struct Node *left, *right; }; /* Recursive function to calculate Tilt of whole tree */ int traverse(Node...
linear
linear
// C++ program to find averages of all levels // 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; }; string inorder(Node* node, unordered_map<string, int>& ...
quadratic
quadratic
// CPP program to find largest three elements in // a binary tree. #include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *left; struct Node *right; }; /* Helper function that allocates a new Node with the given data and NULL left and right pointers. */ struct Node *newNode(int d...
constant
linear