Skip to content

Commit 53e96fb

Browse files
committed
add
1 parent d0644e4 commit 53e96fb

File tree

5 files changed

+297
-0
lines changed

5 files changed

+297
-0
lines changed

010 - BST/011 Largest BST.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
/**********************************************************
3+
4+
Following is the Binary Tree Node class structure
5+
6+
template <typename T>
7+
class BinaryTreeNode {
8+
public:
9+
T data;
10+
BinaryTreeNode<T> *left;
11+
BinaryTreeNode<T> *right;
12+
13+
BinaryTreeNode(T data) {
14+
this->data = data;
15+
left = NULL;
16+
right = NULL;
17+
}
18+
};
19+
20+
***********************************************************/
21+
#include<climits>
22+
#include<cmath>
23+
class Pair{
24+
25+
public:
26+
int minimum;
27+
int maximum;
28+
bool bst;
29+
int height;
30+
};
31+
32+
Pair BST(BinaryTreeNode<int> *root)
33+
{
34+
if(root==NULL)
35+
{
36+
Pair obj;
37+
obj.minimum =INT_MAX;
38+
obj.maximum = INT_MIN;
39+
obj.bst = true;
40+
obj.height=0;
41+
return obj;
42+
}
43+
44+
Pair left= BST(root->left);
45+
Pair right =BST(root->right);
46+
47+
int minimum=min(root->data,min(left.minimum,right.minimum));
48+
int maximum=max(root->data,max(left.maximum,right.maximum));
49+
bool isBSTfinal=(root->data >left.maximum) && (root->data < right.minimum) && left.bst && right.bst;
50+
51+
Pair obj;
52+
obj.minimum=minimum;
53+
obj.maximum=maximum;
54+
obj.bst=isBSTfinal;
55+
if(isBSTfinal)
56+
{
57+
obj.height= 1+max(left.height,right.height);
58+
}
59+
else obj.height= max(left.height,right.height);
60+
return obj;
61+
62+
}
63+
64+
int largestBSTSubtree(BinaryTreeNode<int> *root) {
65+
// Write your code here
66+
return BST(root).height;
67+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**********************************************************
2+
3+
Following is the Binary Tree Node class structure
4+
5+
template <typename T>
6+
class BinaryTreeNode {
7+
public:
8+
T data;
9+
BinaryTreeNode<T> *left;
10+
BinaryTreeNode<T> *right;
11+
12+
BinaryTreeNode(T data) {
13+
this->data = data;
14+
left = NULL;
15+
right = NULL;
16+
}
17+
};
18+
19+
***********************************************************/
20+
int ans = 0;
21+
void replaceWithLargerNodesSum(BinaryTreeNode<int> *root) {
22+
// Write your code here
23+
if(root==NULL){
24+
return;
25+
}
26+
replaceWithLargerNodesSum(root->right);
27+
int val = root->data;
28+
ans+=val;
29+
root->data=ans;
30+
replaceWithLargerNodesSum(root->left);
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**********************************************************
2+
3+
Following is the Binary Tree Node class structure
4+
5+
template <typename T>
6+
class BinaryTreeNode {
7+
public :
8+
T data;
9+
BinaryTreeNode<T> *left;
10+
BinaryTreeNode<T> *right;
11+
12+
BinaryTreeNode(T data) {
13+
this -> data = data;
14+
left = NULL;
15+
right = NULL;
16+
}
17+
};
18+
19+
***********************************************************/
20+
21+
#include<vector>
22+
23+
void rootToLeafPathsSumToK(BinaryTreeNode<int> *root, int k,vector<int> path)
24+
{
25+
if(root==NULL)
26+
return;
27+
28+
path.push_back(root->data);
29+
k=k- root->data;
30+
31+
if(!root->left && !root->right) // leaf node
32+
{
33+
if(k==0)
34+
{
35+
for(int i : path)
36+
{
37+
cout<< i<<" ";
38+
}
39+
cout<<endl;
40+
}
41+
path.pop_back();
42+
return;
43+
}
44+
rootToLeafPathsSumToK(root->left,k,path);
45+
rootToLeafPathsSumToK(root->right,k,path);
46+
47+
}
48+
void rootToLeafPathsSumToK(BinaryTreeNode<int> *root, int k) {
49+
// Write your code here
50+
vector<int> v;
51+
rootToLeafPathsSumToK(root,k,v);
52+
return;
53+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**********************************************************
2+
3+
Following is the Binary Tree Node class structure
4+
5+
template <typename T>
6+
class BinaryTreeNode {
7+
public :
8+
T data;
9+
BinaryTreeNode<T> *left;
10+
BinaryTreeNode<T> *right;
11+
12+
BinaryTreeNode(T data) {
13+
this -> data = data;
14+
left = NULL;
15+
right = NULL;
16+
}
17+
};
18+
19+
***********************************************************/
20+
21+
void printkdistanceNodeDown(BinaryTreeNode<int>*root, int k)
22+
{
23+
// Base Case
24+
if (root == NULL || k < 0) return;
25+
26+
// If we reach a k distant node, print it
27+
if (k==0)
28+
{
29+
cout << root->data << endl;
30+
return;
31+
}
32+
33+
// Recur for left and right subtrees
34+
printkdistanceNodeDown(root->left, k-1);
35+
printkdistanceNodeDown(root->right, k-1);
36+
}
37+
// Prints all nodes at distance k from a given target node.
38+
// The k distant nodes may be upward or downward. This function
39+
// Returns distance of root from target node, it returns -1 if target
40+
// node is not present in tree rooted with root.
41+
42+
// Write your code here
43+
int printkdistanceNode(BinaryTreeNode<int>* root, int target , int k)
44+
{
45+
// Base Case 1: If tree is empty, return -1
46+
if (root == NULL) return -1;
47+
48+
// If target is same as root. Use the downward function
49+
// to print all nodes at distance k in subtree rooted with
50+
// target or root
51+
if (root->data == target)
52+
{
53+
printkdistanceNodeDown(root, k);
54+
return 0;
55+
}
56+
57+
// Recur for left subtree
58+
int dl = printkdistanceNode(root->left, target, k);
59+
60+
// Check if target node was found in left subtree
61+
if (dl != -1)
62+
{
63+
// If root is at distance k from target, print root
64+
// Note that dl is Distance of root's left child from target
65+
if (dl + 1 == k)
66+
cout << root->data << endl;
67+
68+
// Else go to right subtree and print all k-dl-2 distant nodes
69+
// Note that the right child is 2 edges away from left child
70+
else
71+
printkdistanceNodeDown(root->right, k-dl-2);
72+
73+
// Add 1 to the distance and return value for parent calls
74+
return 1 + dl;
75+
}
76+
77+
// MIRROR OF ABOVE CODE FOR RIGHT SUBTREE
78+
// Note that we reach here only when node was not found in left subtree
79+
int dr = printkdistanceNode(root->right, target, k);
80+
if (dr != -1)
81+
{
82+
if (dr + 1 == k)
83+
cout << root->data << endl;
84+
else
85+
printkdistanceNodeDown(root->left, k-dr-2);
86+
return 1 + dr;
87+
}
88+
89+
// If target was neither present in left nor in right subtree
90+
return -1;
91+
}
92+
void nodesAtDistanceK(BinaryTreeNode<int> *root, int node, int k) {
93+
int x= printkdistanceNode(root,node,k);
94+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**********************************************************
2+
3+
Following is the Binary Tree Node class structure
4+
5+
template <typename T>
6+
class BinaryTreeNode {
7+
public:
8+
T data;
9+
BinaryTreeNode<T> *left;
10+
BinaryTreeNode<T> *right;
11+
12+
BinaryTreeNode(T data) {
13+
this->data = data;
14+
left = NULL;
15+
right = NULL;
16+
}
17+
};
18+
19+
***********************************************************/
20+
#include <bits/stdc++.h>
21+
vector<int> arrayPreOrder(BinaryTreeNode<int>* root){
22+
vector<int> temp;
23+
if(root==NULL){
24+
return temp;
25+
}
26+
vector<int> l = arrayPreOrder(root->left);
27+
temp.insert(temp.end(),l.begin(),l.end());
28+
temp.push_back(root->data);
29+
vector<int> r = arrayPreOrder(root->right);
30+
temp.insert(temp.end(),r.begin(),r.end());
31+
return temp;
32+
}
33+
void printData(vector<int> vt,int sum){
34+
int i=0;
35+
int j=vt.size()-1;
36+
while(i<j){
37+
if(vt[i]+vt[j]==sum){
38+
cout<<vt[i]<<" "<<vt[j]<<endl;
39+
i++;
40+
j--;
41+
}else if(vt[i]+vt[j] > sum){
42+
j--;
43+
}else{
44+
i++;
45+
}
46+
}
47+
}
48+
void printNodesSumToS(BinaryTreeNode<int> *root, int s) {
49+
// Write your code here
50+
vector<int> vt = arrayPreOrder(root);
51+
printData(vt,s);
52+
}

0 commit comments

Comments
 (0)