code stringlengths 195 7.9k | space_complexity stringclasses 6
values | time_complexity stringclasses 7
values |
|---|---|---|
// C++ program for an efficient solution to check if
// a given array can represent Preorder traversal of
// a Binary Search Tree
#include<bits/stdc++.h>
using namespace std;
bool canRepresentBST(int pre[], int n)
{
// Create an empty stack
stack<int> s;
// Initialize current root as minimum possible
... | linear | linear |
// C++ program to illustrate if a given array can represent
// a preorder traversal of a BST or not
#include <bits/stdc++.h>
using namespace std;
// We are actually not building the tree
void buildBST_helper(int& preIndex, int n, int pre[],
int min, int max)
{
if (preIndex >= n)
ret... | logn | linear |
// A recursive CPP program to find
// LCA of two nodes n1 and n2.
#include <bits/stdc++.h>
using namespace std;
class node {
public:
int data;
node *left, *right;
};
/* Function to find LCA of n1 and n2.
The function assumes that both
n1 and n2 are present in BST */
node* lca(node* root, int n1, int n2)
{
... | logn | constant |
// A recursive CPP program to find
// LCA of two nodes n1 and n2.
#include <bits/stdc++.h>
using namespace std;
class node {
public:
int data;
node *left, *right;
};
/* Function to find LCA of n1 and n2.
The function assumes that both n1 and n2
are present in BST */
node* lca(node* root, int n1, int n2)
{
... | constant | constant |
#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;
/* Constructor that allocates
a new node with the given data
and NULL left and right pointers. */
no... | constant | linear |
// C++ program to check if a given tree is BST.
#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;
Node(int data)
{
this->data = data;
left = right = NU... | logn | linear |
// A C++ program to check for Identical
// BSTs without building the trees
#include <bits/stdc++.h>
using namespace std;
/* The main function that checks if two
arrays a[] and b[] of size n construct
same BST. The two values 'min' and 'max'
decide whether the call is made for left
subtree or right subtree of a parent... | linear | quadratic |
// CPP code for finding K-th largest Node using O(1)
// extra memory and reverse Morris traversal.
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
// helper function to create a new Node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data =... | constant | linear |
// C++ program to find k'th largest element in BST
#include<bits/stdc++.h>
using namespace std;
// A BST node
struct Node
{
int key;
Node *left, *right;
};
// A function to find
int KSmallestUsingMorris(Node *root, int k)
{
// Count to iterate over elements till we
// get the kth smallest number
... | constant | linear |
// C++ program to check if a given array is sorted
// or not.
#include<bits/stdc++.h>
using namespace std;
// Function that returns true if array is Inorder
// traversal of any Binary Search Tree or not.
bool isInorder(int arr[], int n)
{
// Array has one or no element
if (n == 0 || n == 1)
return tru... | constant | linear |
// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
// BST Node
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
// Utility function to create new Node
Node* newNode(int val)
{
Node* temp = new Node;
temp->data = val;... | linear | linear |
// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
// BST Node
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
// Utility function to create new Node
Node* newNode(int val)
{
Node* temp = new Node;
temp->data = val;... | linear | linear |
// C++ implementation to count pairs from two
// BSTs whose sum is equal to a given value x
#include <bits/stdc++.h>
using namespace std;
// structure of a node of BST
struct Node {
int data;
Node* left, *right;
};
// function to create and return a node of BST
Node* getNode(int data)
{
// allocate spa... | logn | linear |
/* C++ program to find the median of BST in O(n)
time and O(1) space*/
#include<bits/stdc++.h>
using namespace std;
/* A binary search tree Node has data, pointer
to left child and a pointer to right child */
struct Node
{
int data;
struct Node* left, *right;
};
// A utility function to create a new B... | constant | linear |
// C++ program to find largest BST 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;
struct Node* right;
};
/* Helper function that allocates a new
node with ... | linear | linear |
// A C++ program to remove BST keys
// outside the given range
#include<bits/stdc++.h>
using namespace std;
// A BST node has key, and left and right pointers
struct node
{
int key;
struct node *left;
struct node *right;
};
// Removes all nodes having value outside the
// given range and returns the r... | constant | linear |
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
/* A tree node structure */
class node
{
public:
int data;
node *left;
node *right;
};
/* The functions prints all the keys
which in the given range [k1..k2].
The function assumes than k1 < k2 */
void Print(n... | logn | linear |
// CPP code to print BST keys in given Range in
// constant space using Morris traversal.
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node *left, *right;
};
// Function to print the keys in range
void RangeTraversal(node* root,
int n1, int n2)
{
i... | constant | linear |
// C++ program to count BST nodes within a given range
#include<bits/stdc++.h>
using namespace std;
// A BST node
struct node
{
int data;
struct node* left, *right;
};
// Utility function to create new node
node *newNode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = temp->... | linear | constant |
// c++ program to find Sum Of All Elements smaller
// than or equal to Kth Smallest Element In BST
#include <bits/stdc++.h>
using namespace std;
/* Binary tree Node */
struct Node
{
int data;
Node* left, * right;
};
// utility function new Node of BST
struct Node *createNode(int data)
{
Node * new_Node ... | logn | constant |
// C++ program to find predecessor and successor in a BST
#include <iostream>
using namespace std;
// BST Node
struct Node
{
int key;
struct Node *left, *right;
};
// This function finds predecessor and successor of key in BST.
// It sets pre and suc as predecessor and successor respectively
void findPreSuc... | constant | logn |
// CPP code for inorder successor
// and predecessor of tree
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node
{
int data;
Node* left,*right;
};
// Function to return data
Node* getnode(int info)
{
Node* p = (Node*)malloc(sizeof(Node));
p->data = info;
p->right = NULL;
... | linear | linear |
// C++ program to find predecessor
// and successor in a BST
#include <bits/stdc++.h>
using namespace std;
// BST Node
struct Node {
int key;
struct Node *left, *right;
};
// Function that finds predecessor and successor of key in BST.
void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
{
if (r... | constant | linear |
// CPP program to find a pair with
// given sum using hashing
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* NewNode(int data)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
... | linear | linear |
// C++ program to find pairs with given sum such
// that one element of pair exists in one BST and
// other in other BST.
#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 BST node
// with key as giv... | linear | linear |
// Recursive C++ program to find key closest to k
// in given Binary Search Tree.
#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;
};
/* Utility that allocates a new node with t... | constant | constant |
// C++ program to find largest BST 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;
struct Node* right;
Node(int val)
{
this->data = val;
... | linear | linear |
// C++ program to add all greater
// values in every node of BST
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *left, *right;
};
// A utility function to create
// a new BST node
Node* newNode(int item)
{
Node* temp = new Node();
temp->data = item;
temp->left ... | constant | linear |
/* C++ program to convert a Binary Tree to Threaded Tree */
#include <bits/stdc++.h>
using namespace std;
/* Structure of a node in threaded binary tree */
struct Node {
int key;
Node *left, *right;
// Used to indicate whether the right pointer is a
// normal right pointer or a pointer to inorder
... | linear | linear |
/* C++ program to convert a Binary Tree to
Threaded Tree */
#include <bits/stdc++.h>
using namespace std;
/* Structure of a node in threaded binary tree */
struct Node
{
int key;
Node *left, *right;
// Used to indicate whether the right pointer
// is a normal right pointer or a pointer
// to... | constant | linear |
// C++ program to print BST in given range
#include<bits/stdc++.h>
using namespace std;
/* A Binary Tree node */
class TNode
{
public:
int data;
TNode* left;
TNode* right;
};
TNode* newNode(int data);
/* A function that constructs Balanced
Binary Search Tree from a sorted array */
TNode* sortedArr... | logn | 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 |
// C++ Code for the above approach
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data,
a pointer to left child
and a pointer to right child */
class Node {
public:
int data;
Node* left;
Node* right;
};
// Function to return a new Node
Node* newNode(int data)
{
Node* node ... | constant | linear |
// Two nodes in the BST's swapped, correct the BST.
#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;
};
// A utility function to swap two integers
void swap( int* a, int* ... | linear | linear |
// C++ Program to find ceil of a given value in BST
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has key, left child and right child */
class node {
public:
int key;
node* left;
node* right;
};
/* Helper function that allocates a new node with the given
key and NULL left and right... | logn | logn |
// C++ Program to find floor of a given value in BST
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has key, left child and right child */
class node {
public:
int key;
node* left;
node* right;
};
/* Helper function that allocates a new node with the given
key and NULL left and righ... | logn | logn |
// C++ program to find floor and ceil of a given key in BST
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has key, left child and right child */
struct Node {
int data;
Node *left, *right;
Node(int value)
{
data = value;
left = right = NULL;
}
};
// Helper... | constant | logn |
// C++ program of iterative traversal based method to
// find common elements in two BSTs.
#include <iostream>
#include <stack>
using namespace std;
// A BST node
struct Node {
int key;
struct Node *left, *right;
};
// A utility function to create a new node
Node* newNode(int ele)
{
Node* temp = new N... | logn | linear |
// An AVL Tree based C++ program to count
// inversion in an array
#include<bits/stdc++.h>
using namespace std;
// An AVL tree node
struct Node
{
int key, height;
struct Node *left, *right;
// size of the tree rooted with this Node
int size;
};
// A utility function to get the height of
// the tr... | linear | nlogn |
// C++ program to print leaf node from
// preorder of binary search tree.
#include<bits/stdc++.h>
using namespace std;
// Binary Search
int binarySearch(int inorder[], int l, int r, int d)
{
int mid = (l + r)>>1;
if (inorder[mid] == d)
return mid;
else if (inorder[mid] > d)
return bina... | linear | nlogn |
// CPP program to find number of pairs and minimal
// possible value
#include <bits/stdc++.h>
using namespace std;
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
// initialize smallest and count
int smallest = INT_MAX;
int count = 0;
// iterate over all pairs
... | constant | quadratic |
// C++ program to find number of pairs
// and minimal possible value
#include <bits/stdc++.h>
using namespace std;
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
// initialize smallest and count
int smallest = INT_MAX, count = 0;
set<int> s;
// iterate over all pai... | linear | nlogn |
// CPP program to find rank of an
// element in a stream.
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
int leftSize;
};
Node* newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
temp->leftSize = 0;
... | linear | linear |
// C++ program to find rank of an
// element in a stream.
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int a[] = {5, 1, 14, 4, 15, 9, 7, 20, 11};
int key = 20;
int arraySize = sizeof(a)/sizeof(a[0]);
int count = 0;
for(int i = 0; i < arraySize; i++)
{
if(a[... | constant | linear |
// C++ program to insert element in Binary Tree
#include <iostream>
#include <queue>
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;
Node* right;
};
// Function to create a new node
Node* CreateNode(int data... | logn | linear |
// C++ program to delete element in binary tree
#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;
};
/* function to create a new node of tree and
return pointer */
struct Node* ... | linear | linear |
// C++ program to delete element in binary tree
#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 data;
struct Node *left, *right;
};
/* function to create a new node of tree and
return pointer */
struct Node*... | linear | linear |
// C++ implementation of tree using array
// numbering starting from 0 to n-1.
#include<bits/stdc++.h>
using namespace std;
char tree[10];
int root(char key) {
if (tree[0] != '\0')
cout << "Tree already had root";
else
tree[0] = key;
return 0;
}
int set_left(char key, int parent) {
if (tree[parent] ==... | linear | logn |
// C++ program to evaluate an expression tree
#include <bits/stdc++.h>
using namespace std;
// Class to represent the nodes of syntax tree
class node
{
public:
string info;
node *left = NULL, *right = NULL;
node(string x)
{
info = x;
}
};
// Utility function to return the integer value
/... | constant | 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;
t... | logn | linear |
// C++ program to print inorder traversal
// using stack.
#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 data)
{
this->data = data... | linear | linear |
// C++ program for finding postorder
// traversal of BST from preorder traversal
#include <bits/stdc++.h>
using namespace std;
// Function to find postorder traversal from
// preorder traversal.
void findPostOrderUtil(int pre[], int n, int minval,
int maxval, int& preIndex)
{
// If entire... | 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* 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 |
// Recursive CPP program for level
// order traversal 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, *right;
};
/* Function prototypes */
void printCurrentLevel(node... | linear | quadratic |
/* C++ program to print level
order traversal using STL */
#include <bits/stdc++.h>
using namespace std;
// A Binary Tree Node
struct Node {
int data;
struct Node *left, *right;
};
// Iterative method to find height of Binary Tree
void printLevelOrder(Node* root)
{
// Base Case
if (root == NULL)... | linear | linear |
// C++ program for recursive level
// order traversal in spiral form
#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;
};
// Function prototypes
void printG... | linear | quadratic |
// C++ implementation of a O(n) time method for spiral order
// traversal
#include <iostream>
#include <stack>
using namespace std;
// Binary Tree node
struct node {
int data;
struct node *left, *right;
};
void printSpiral(struct node* root)
{
if (root == NULL)
return; // NULL check
// Cre... | linear | linear |
// C++ implementation of above approach
#include <iostream>
#include <stack>
using namespace std;
// Binary Tree node
struct Node {
int key;
struct Node *left, *right;
};
void spiralPrint(struct Node* root)
{
// Declare a deque
deque<Node*> dq;
// Insert the root of the tree into the deque
... | linear | linear |
/* C++ program for special order traversal */
#include <iostream>
#include <queue>
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;
Node *right;
};
/* Helper function that allocates a new node with the
gi... | linear | linear |
// C++ program to reverse
// alternate levels of a tree
#include <bits/stdc++.h>
using namespace std;
struct Node
{
char key;
Node *left, *right;
};
void preorder(struct Node *root1, struct Node*
root2, int lvl)
{
// Base cases
if (root1 == NULL || root2==NULL)
... | logn | linear |
// C++ program to implement iterative preorder traversal
#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;
};
/* Helper function that allocates a new node with the given data and
... | logn | linear |
#include <bits/stdc++.h>
using namespace std;
// Tree Node
struct Node {
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = NULL;
}
};
// Iterative function to do Preorder traversal of the tree
void preorderIterative(Node* root)
{
i... | logn | linear |
// C++ program for diagonal
// traversal of Binary Tree
#include <bits/stdc++.h>
using namespace std;
// Tree node
struct Node
{
int data;
Node *left, *right;
};
/* root - root of the binary tree
d - distance of current line from rightmost
-topmost slope.
diagonalPrint - multimap to store Dia... | linear | nlogn |
#include <bits/stdc++.h>
using namespace std;
// Tree node
struct Node {
int data;
Node *left, *right;
};
vector<int> diagonal(Node* root)
{
vector<int> diagonalVals;
if (!root)
return diagonalVals;
// The leftQueue will be a queue which will store all
// left pointers while traver... | linear | nlogn |
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return node;
}
vector <vector <int>> result;
void diagonalPrint(Node* root)
{
if(root == NUL... | linear | linear |
/* C++ program to print the diagonal traversal 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 data;
Node *left, *right;
};
/* Helper function that allocates a new node */
Node* newNo... | linear | linear |
#include <iostream>
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 function to create a new tree node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data;
t... | linear | linear |
#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 function to create a new tree node
Node* newNode(int data)
{
Node* temp = new Node;
temp->data = data... | linear | linear |
//C++ program to find density of a binary tree
#include<bits/stdc++.h>
// A binary tree node
struct Node
{
int data;
Node *left, *right;
};
// Helper function to allocates a new node
Node* newNode(int data)
{
Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return n... | logn | linear |
// C++ program to find height of full binary tree
// using preorder
#include <bits/stdc++.h>
using namespace std;
// function to return max of left subtree height
// or right subtree height
int findDepthRec(char tree[], int n, int& index)
{
if (index >= n || tree[index] == 'l')
return 0;
// calc hei... | constant | linear |
// C code to modify binary tree for
// traversal using only right pointer
#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;
};
// function that allocates a new node
// with the give... | linear | linear |
// C++ code to modify binary tree for
// traversal using only right pointer
#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.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;
};
/... | linear | linear |
/* C++ program to construct tree using
inorder and preorder traversals */
#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:
char data;
node* left;
node* right;
};
/* Prototypes for utility function... | linear | quadratic |
/* C++ program to construct tree using inorder
and preorder traversals */
#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 {
char data;
struct Node* left;
struct Node* right;
};
struct Node* newNode(char da... | linear | linear |
// C++ program to construct a tree using
// inorder and preorder traversal
#include<bits/stdc++.h>
using namespace std;
class TreeNode
{
public:
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) { val = x; }
};
set<TreeNode*> s;
stack<TreeNode*> st;
// Function to build tree using give... | linear | linear |
/* program to construct tree using inorder and levelorder
* traversals */
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node */
struct Node {
int key;
struct Node *left, *right;
};
/* Function to find index of value in arr[start...end] */
int search(int arr[], int strt, int end, int value)... | linear | cubic |
// C++ program to create a Complete Binary tree from its Linked List
// Representation
#include <iostream>
#include <string>
#include <queue>
using namespace std;
// Linked list node
struct ListNode
{
int data;
ListNode* next;
};
// Binary tree node structure
struct BinaryTreeNode
{
int data;
Binary... | np | linear |
// CPP program to construct binary
// tree from given array in level
// order fashion Tree Node
#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;
};
/* Helper function that allo... | linear | linear |
// Program for construction of Full 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;
};
// A utility function to create a node
node* newNode(int da... | logn | constant |
// C++ program to construct full binary tree
// using its preorder traversal and preorder
// traversal of its mirror 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... | linear | quadratic |
// A program to construct Binary Tree from preorder traversal
#include<bits/stdc++.h>
// A binary tree node structure
struct node
{
int data;
struct node *left;
struct node *right;
};
// Utility function to create a new Binary Tree node
struct node* newNode (int data)
{
struct node *temp = new struc... | linear | linear |
// A program to construct Binary Tree from preorder
// traversal
#include <bits/stdc++.h>
using namespace std;
// A binary tree node structure
struct node {
int data;
struct node* left;
struct node* right;
node(int x)
{
data = x;
left = right = NULL;
}
};
struct node* constru... | linear | linear |
// Given an ancestor matrix for binary tree, construct
// the tree.
#include <bits/stdc++.h>
using namespace std;
# define N 6
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
/* Helper function to create a new node */
Node* newNode(int data)
{
Node* node = new Node;
n... | quadratic | quadratic |
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
// Creating a global boolean matrix for simplicity
bool mat[MAX][MAX];
// anc[] stores all ancestors of curren... | quadratic | quadratic |
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define size 6
int M[size][size]={0};
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
/* Helper function to create a new node */
Node* newnode(int data)
{
Node* node = ne... | quadratic | quadratic |
/* C++ program to construct tree
from inorder traversal */
#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;
};
/* Prototypes of a utility function to get the max... | linear | quadratic |
// C++ program to construct a Binary Tree from parent array
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left = NULL;
struct Node* right = NULL;
Node() {}
Node(int x) { data = x; }
};
// Function to construct binary tree from parent array.
Node* createTr... | linear | linear |
/* C++ program to construct tree using inorder and
postorder traversals */
#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;
};
// Utility function to create a new node
Node*... | linear | quadratic |
/* C++ program to construct tree using inorder and
postorder traversals */
#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;
};
// Utility function to create a new node
Node* newNo... | linear | linear |
// C++ program for 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 data;
Node *left, *right;
Node(int x)
{
data = x;
left = right = NULL;
}
};
/*Tree building function*/
Node *bui... | linear | linear |
// C++ program to create a doubly linked list out
// of given a ternary tree.
#include <bits/stdc++.h>
using namespace std;
/* A ternary tree */
struct Node
{
int data;
struct Node *left, *middle, *right;
};
/* Helper function that allocates a new node with the
given data and assign NULL to left, middle ... | linear | linear |
// C++ program to create a tree with left child
// right sibling representation.
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
struct Node* child;
};
// Creating new Node
Node* newNode(int data)
{
Node* newNode = new Node;
newNode->next = newNode->child... | linear | linear |
/* C++ program to construct a binary tree from
the given string */
#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;
};
/* Helper function that allocates a new node */
Node* newN... | linear | quadratic |
// A simple inorder traversal based
// program to convert a Binary Tree to DLL
#include <bits/stdc++.h>
using namespace std;
// A tree node
class node
{
public:
int data;
node *left, *right;
};
// A utility function to create
// a new tree node
node *newNode(int data)
{
node *Node = new node();
... | linear | linear |
// A C++ program for in-place conversion of Binary Tree to
// DLL
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, and left and right pointers
*/
struct node {
int data;
node* left;
node* right;
};
node * bToDLL(node *root)
{
stack<pair<node*, int>> s;
s.push({root,... | linear | linear |
// C++ program to convert a given Binary Tree to Doubly Linked List
#include <bits/stdc++.h>
// Structure for tree and linked list
struct Node {
int data;
Node *left, *right;
};
// Utility function for allocating node for Binary
// Tree.
Node* newNode(int data)
{
Node* node = new Node;
node->data ... | linear | linear |
// C++ program to convert a binary tree
// to its mirror
#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 allocates a new node with... | linear | linear |
// Iterative CPP program to convert a Binary
// Tree to its mirror
#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 allocates a ... | linear | linear |
/* c++ program to convert Binary Tree into Doubly
Linked List where the nodes are represented
spirally. */
#include <bits/stdc++.h>
using namespace std;
// A Binary Tree Node
struct Node
{
int data;
struct Node *left, *right;
};
/* Given a reference to the head of a list and a node,
inserts the node... | linear | linear |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.