
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Unique Binary Search Trees II in C++
Suppose we have an integer n, we have to generate all structurally unique binary search trees that store values from 1 to n. So if the input is 3, then the trees will be −
To solve this, we will follow these steps −
- Define one recursive function called generate(), this will take low and high
- define one tree node called temp.
- if low > high, then insert null into the temp, and return temp
- for i in range low to high
- left_subtree := generate(low, i – 1)
- right_subtree := generate(i + 1, high)
- current := i
- for j in range 0 to size of the left_subtree
- for k in range 0 to size of the right_subtree
- curr_node := make one tree node with value current
- left of the curr_node := left_subtree[j]
- right of the curr_node := right_subtree[k]
- insert curr_node into the temp
- for k in range 0 to size of the right_subtree
- return temp.
- Initially call the generate() function with values 1 and n to generate all trees.
Example(C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; } else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; } else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } void tree_level_trav(TreeNode*root){ if (root == NULL) return; cout << "["; queue<TreeNode *> q; TreeNode *curr; q.push(root); q.push(NULL); while (q.size() > 1) { curr = q.front(); q.pop(); if (curr == NULL){ q.push(NULL); } else { if(curr->left) q.push(curr->left); if(curr->right) q.push(curr->right); if(curr == NULL || curr->val == 0){ cout << "null" << ", "; } else{ cout << curr->val << ", "; } } } cout << "]"<<endl; } class Solution { public: vector<TreeNode*> generate(int low, int high) { vector <TreeNode*> temp; if(low > high){ temp.push_back(NULL); return temp; } for(int i = low;i<=high;i++){ vector <TreeNode*> leftSubtree = generate(low,i-1); vector <TreeNode*> rightSubtree = generate(i+1,high); int current = i; for(int j = 0;j<leftSubtree.size();j++){ for(int k =0;k<rightSubtree.size();k++){ TreeNode* currentNode = new TreeNode(current); currentNode->left = leftSubtree[j]; currentNode->right = rightSubtree[k]; temp.push_back(currentNode); } } } return temp; } vector<TreeNode*> generateTrees(int n) { if(!n){ vector <TreeNode*> r; return r; } return generate(1,n) ; } }; main(){ Solution ob; vector<TreeNode*> v = ob.generateTrees(3); for(int i = 0; i<v.size(); i++) tree_level_trav(v[i]); }
Input
3
Output
[1, 2, 3, ] [1, 3, 2, ] [2, 1, 3, ] [3, 1, 2, ] [3, 2, 1, ]
- Related Articles
- Unique Binary Search Trees in C++
- Multidimensional Binary Search Trees
- Binary Search Trees in Data Structures
- Optimal Binary Search Trees in Data Structures
- Balanced binary search trees in Data Structure
- All Elements in Two Binary Search Trees in C++
- Print Common Nodes in Two Binary Search Trees in C++
- Count the Number of Binary Search Trees present in a Binary Tree in C++
- Closest Binary Search Tree Value II in C++
- Comparison of Search Trees in Data Structure
- Dynamic Finger Search Trees in Data Structure
- Randomized Finger Search Trees in Data Structure
- Flip Equivalent Binary Trees in C++
- Merge Two Binary Trees in C++
- Threaded Binary Trees in Data Structure

Advertisements