Skip to content

Commit 777f1eb

Browse files
committed
Added Tree Problems in Java and CPP
1 parent d83e381 commit 777f1eb

10 files changed

+776
-0
lines changed

Trees/KthLargestElementInBST.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given a Binary search tree. Your task is to complete the function which will return the Kth largest element without doing any modification in Binary Search Tree.
2+
3+
4+
// Example 1:
5+
6+
// Input:
7+
// 4
8+
// / \
9+
// 2 9
10+
// k = 2
11+
// Output: 4
12+
13+
// Example 2:
14+
15+
// Input:
16+
// 9
17+
// \
18+
// 10
19+
// K = 1
20+
// Output: 10
21+
22+
// Your Task:
23+
// You don't need to read input or print anything. Your task is to complete the function kthLargest() which takes the root of the BST and an integer K as inputs and returns the Kth largest element in the given BST.
24+
25+
26+
// Expected Time Complexity: O(H + K).
27+
// Expected Auxiliary Space: O(H)
28+
29+
30+
// Constraints:
31+
// 1 <= N <= 1000
32+
// 1 <= K <= N
33+
34+
class Solution
35+
{
36+
public:
37+
int kthLargest(Node *root, int k)
38+
{
39+
stack<Node*> st;
40+
while(!st.empty() || root){
41+
while(root){
42+
st.push(root);
43+
root = root->right;
44+
}
45+
root = st.top(); st.pop();
46+
if(--k == 0)
47+
return root->data;
48+
else root = root->left;
49+
}
50+
return -1;
51+
}
52+
};

Trees/KthLargestElementInBST.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Given a Binary search tree. Your task is to complete the function which will return the Kth largest element without doing any modification in Binary Search Tree.
2+
3+
4+
// Example 1:
5+
6+
// Input:
7+
// 4
8+
// / \
9+
// 2 9
10+
// k = 2
11+
// Output: 4
12+
13+
// Example 2:
14+
15+
// Input:
16+
// 9
17+
// \
18+
// 10
19+
// K = 1
20+
// Output: 10
21+
22+
// Your Task:
23+
// You don't need to read input or print anything. Your task is to complete the function kthLargest() which takes the root of the BST and an integer K as inputs and returns the Kth largest element in the given BST.
24+
25+
26+
// Expected Time Complexity: O(H + K).
27+
// Expected Auxiliary Space: O(H)
28+
29+
30+
// Constraints:
31+
// 1 <= N <= 1000
32+
// 1 <= K <= N
33+
34+
/*
35+
class Node
36+
{
37+
int data;
38+
Node left;
39+
Node right;
40+
Node(int data)
41+
{
42+
this.data = data;
43+
left=null;
44+
right=null;
45+
}
46+
}
47+
*/
48+
class Solution
49+
{
50+
// return the Kth largest element in the given BST rooted at 'root'
51+
public int kthLargest(Node root,int k)
52+
{
53+
Stack<Node> st = new Stack<>();
54+
while(root != null || !st.isEmpty()){
55+
while(root != null){
56+
st.push(root);
57+
root = root.right;
58+
}
59+
root = st.pop();
60+
if(--k == 0) return root.data;
61+
else root = root.left;
62+
}
63+
return -1;
64+
}
65+
}

Trees/KthSmallestElementInBST.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the
2+
// values of the nodes in the tree.
3+
4+
// Example 1:
5+
// Input: root = [3,1,4,null,2], k = 1
6+
// Output: 1
7+
// Example 2:
8+
// Input: root = [5,3,6,2,4,null,null,1], k = 3
9+
// Output: 3
10+
11+
// Constraints:
12+
// The number of nodes in the tree is n.
13+
// 1 <= k <= n <= 104
14+
// 0 <= Node.val <= 104
15+
16+
// Follow up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the
17+
// kth smallest frequently, how would you optimize?
18+
19+
// Method 1
20+
21+
/**
22+
* Definition for a binary tree node.
23+
* struct TreeNode {
24+
* int val;
25+
* TreeNode *left;
26+
* TreeNode *right;
27+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
28+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
29+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
30+
* };
31+
*/
32+
class Solution {
33+
public:
34+
vector<int> v;
35+
void solve(TreeNode *root, vector<int> &v)
36+
{
37+
if(root==NULL)return;
38+
solve(root->left, v);
39+
v.push_back(root->val);
40+
solve(root->right, v);
41+
return;
42+
}
43+
int kthSmallest(TreeNode* root, int k) {
44+
solve(root, v);
45+
return v[k-1];
46+
}
47+
};
48+
49+
// Method 2
50+
51+
class Solution {
52+
public:
53+
int kthSmallest(TreeNode* root, int k) {
54+
stack<TreeNode*> st;
55+
while(root || !st.empty()){
56+
while(root){
57+
st.push(root);
58+
root = root->left;
59+
}
60+
root = st.top(); st.pop();
61+
if(--k == 0) return root->val;
62+
else root = root->right;
63+
}
64+
return -1;
65+
}
66+
};

Trees/KthSmallestElementInBST.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the
2+
// values of the nodes in the tree.
3+
4+
// Example 1:
5+
// Input: root = [3,1,4,null,2], k = 1
6+
// Output: 1
7+
// Example 2:
8+
// Input: root = [5,3,6,2,4,null,null,1], k = 3
9+
// Output: 3
10+
11+
// Constraints:
12+
// The number of nodes in the tree is n.
13+
// 1 <= k <= n <= 104
14+
// 0 <= Node.val <= 104
15+
16+
// Follow up: If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the
17+
// kth smallest frequently, how would you optimize?
18+
19+
/**
20+
* Definition for a binary tree node.
21+
* public class TreeNode {
22+
* int val;
23+
* TreeNode left;
24+
* TreeNode right;
25+
* TreeNode() {}
26+
* TreeNode(int val) { this.val = val; }
27+
* TreeNode(int val, TreeNode left, TreeNode right) {
28+
* this.val = val;
29+
* this.left = left;
30+
* this.right = right;
31+
* }
32+
* }
33+
*/
34+
class Solution {
35+
public int kthSmallest(TreeNode root, int k) {
36+
Stack<TreeNode> st = new Stack<>();
37+
while(root != null || !st.isEmpty()){
38+
while(root != null){
39+
st.push(root);
40+
root = root.left;
41+
}
42+
root = st.pop();
43+
if(--k == 0) return root.val;
44+
else root = root.right;
45+
}
46+
return -1;
47+
}
48+
}

Trees/LargestBSTInABinaryTree.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Given a binary tree. Find the size of its largest subtree that is a Binary Search Tree.
2+
// Note: Here Size is equal to the number of nodes in the subtree.
3+
4+
// Example 1:
5+
6+
// Input:
7+
// 1
8+
// / \
9+
// 4 4
10+
// / \
11+
// 6 8
12+
// Output: 1
13+
// Explanation: There's no sub-tree with size
14+
// greater than 1 which forms a BST. All the
15+
// leaf Nodes are the BSTs with size equal
16+
// to 1.
17+
// Example 2:
18+
19+
// Input: 6 6 3 N 2 9 3 N 8 8 2
20+
// 6
21+
// / \
22+
// 6 3
23+
// \ / \
24+
// 2 9 3
25+
// \ / \
26+
// 8 8 2
27+
// Output: 2
28+
// Explanation: The following sub-tree is a
29+
// BST of size 2:
30+
// 2
31+
// / \
32+
// N 8
33+
// Your Task:
34+
// You don't need to read input or print anything. Your task is to complete the function largestBst() that takes the root node of the Binary Tree as its input and returns the size of the largest subtree which is also the BST. If the complete Binary Tree is a BST, return the size of the complete Binary Tree.
35+
36+
// Expected Time Complexity: O(N).
37+
// Expected Auxiliary Space: O(Height of the BST).
38+
39+
// Constraints:
40+
// 1 ≤ Number of nodes ≤ 105
41+
// 1 ≤ Data of a node ≤ 106
42+
/*
43+
struct Node {
44+
int data;
45+
Node *left;
46+
Node *right;
47+
48+
Node(int val) {
49+
data = val;
50+
left = right = NULL;
51+
}
52+
};*/
53+
54+
class Solution{
55+
public:
56+
vector<int> solve(Node *root){
57+
if(!root) return {1, 0, INT_MAX, INT_MIN};
58+
if(!root->left and !root->right) return {1, 1, root->data, root->data};
59+
60+
vector<int> l = solve(root->left);
61+
vector<int> r = solve(root->right);
62+
if(l[0] and r[0]){
63+
if(root->data > l[3] and root->data < r[2]){
64+
int x = l[2], y = r[3];
65+
if(x == INT_MAX) x = root->data;
66+
if(y == INT_MIN) y = root->data;
67+
return {1, l[1] + r[1] + 1, x, y};
68+
}
69+
}
70+
return {0, max(l[1], r[1]), 0, 0};
71+
}
72+
73+
int largestBst(Node *root)
74+
{
75+
vector<int> ans = solve(root);
76+
return ans[1];
77+
}
78+
};

Trees/LargestBSTInABinaryTree.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Given a binary tree. Find the size of its largest subtree that is a Binary Search Tree.
2+
// Note: Here Size is equal to the number of nodes in the subtree.
3+
4+
// Example 1:
5+
6+
// Input:
7+
// 1
8+
// / \
9+
// 4 4
10+
// / \
11+
// 6 8
12+
// Output: 1
13+
// Explanation: There's no sub-tree with size
14+
// greater than 1 which forms a BST. All the
15+
// leaf Nodes are the BSTs with size equal
16+
// to 1.
17+
// Example 2:
18+
19+
// Input: 6 6 3 N 2 9 3 N 8 8 2
20+
// 6
21+
// / \
22+
// 6 3
23+
// \ / \
24+
// 2 9 3
25+
// \ / \
26+
// 8 8 2
27+
// Output: 2
28+
// Explanation: The following sub-tree is a
29+
// BST of size 2:
30+
// 2
31+
// / \
32+
// N 8
33+
// Your Task:
34+
// You don't need to read input or print anything. Your task is to complete the function largestBst() that takes the root node of the Binary Tree as its input and returns the size of the largest subtree which is also the BST. If the complete Binary Tree is a BST, return the size of the complete Binary Tree.
35+
36+
// Expected Time Complexity: O(N).
37+
// Expected Auxiliary Space: O(Height of the BST).
38+
39+
// Constraints:
40+
// 1 ≤ Number of nodes ≤ 105
41+
// 1 ≤ Data of a node ≤ 106
42+
43+
// To do

0 commit comments

Comments
 (0)