Skip to content

Commit 37b25e7

Browse files
committed
Kth smallest element in binary search tree problem
1 parent 0b85ff9 commit 37b25e7

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 161 |
9+
| Total Problems | 162 |
1010

1111
</center>
1212

@@ -111,7 +111,7 @@ Include contains single header implementation of data structures and some algori
111111
|Recursive Level order traveral of Tree | [levelOrderTraversalRecursive.cpp](tree_problems/levelOrderTraversalRecursive.cpp)|
112112
|ZigZag Traversal of Tree | [zigZagTraversal.cpp](tree_problems/zigZagTraversal.cpp)|
113113
|Predecessor and Successor of a given node in Binary Search Tree | [predecessorSuccessor.cpp](tree_problems/predecessorSuccessor.cpp)|
114-
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp](tree_problems/lowest-common-ancestor.cpp)|
114+
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp] (tree_problems/lowest-common-ancestor.cpp)|
115115
|Given a binary tree (unlike binary search tree), find the Lowest Common Ancestor (LCA).|[lowest-common-ancestor-binary-tree.cpp](tree_problems/lowest-common-ancestor-binary-tree.cpp)|
116116
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
117117
|Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.| [sumTree.cpp](tree_problems/sumTree.cpp)|
@@ -124,6 +124,7 @@ Include contains single header implementation of data structures and some algori
124124
| Given a binary tree, print its nodes level by level in reverse order. i.e. all nodes present at last level should be printed first followed by nodes of second-last level and so on.. All nodes for any level should be printed from left to right. | [reverseLevelOrderTraversal.cpp](tree_problems/reverseLevelOrderTraversal.cpp) |
125125
| Invert a binary tree, recursively and iteratively.| [invert_a_tree.cpp](tree_problems/invert_a_tree.cpp) |
126126
| Given a Binary Search Tree, find ceil and floor of a given key in it. If the given key lie in the BST, then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any) in the BST and floor is equal to previous greater key (if any) in the BST | [floor_ceil_bst.cpp](tree_problems/floor_ceil_bst.cpp) |
127+
| Find kth smallest element in a binary search tree | [kth_smallest.cpp](tree_problems/kth_smallest.cpp)|
127128

128129
### String Problems
129130
| Problem | Solution |

tree_problems/kth_smallest.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Given a binary search tree, find the kth smallest element in it.
3+
* Assumption: k is always valid i.e. 1 <= k <= total number of nodes in BST
4+
*
5+
*         8
6+
 *        /   \
7+
 *       /     \
8+
 *      4      10
9+
 *     / \     / \
10+
 *    /   \   /   \
11+
 *   2     6 9    12
12+
* k = 2, answer: 4
13+
* k = 7, answer: 12
14+
*/
15+
16+
#include <iostream>
17+
18+
struct TreeNode {
19+
int data;
20+
TreeNode* left;
21+
TreeNode* right;
22+
TreeNode(int d):
23+
data{d}, left{nullptr}, right{nullptr} { }
24+
};
25+
26+
int countNodes(TreeNode* root)
27+
{
28+
if (root == nullptr) {
29+
return 0;
30+
}
31+
return 1 + countNodes(root->left) + countNodes(root->right);
32+
}
33+
34+
int kthSmallest(TreeNode* root, int k)
35+
{
36+
int count = countNodes(root->left);
37+
if (k <= count && root->left) {
38+
return kthSmallest(root->left, k);
39+
} else if ( k + 1 >= count && root->right) {
40+
return kthSmallest(root->right, k - count - 1);
41+
}
42+
return root->data;
43+
}
44+
45+
void insertNode(TreeNode* &root, int d)
46+
{
47+
if (!root) {
48+
TreeNode* newNode = new TreeNode(d);
49+
root = newNode;
50+
return;
51+
}
52+
if (root->data >= d) {
53+
insertNode(root->left, d);
54+
}
55+
else {
56+
insertNode(root->right, d);
57+
}
58+
}
59+
60+
void inorder(TreeNode* root)
61+
{
62+
if (root) {
63+
inorder(root->left);
64+
std::cout << root->data << " ";
65+
inorder(root->right);
66+
}
67+
}
68+
69+
int main()
70+
{
71+
TreeNode* root = nullptr;
72+
insertNode(root, 8);
73+
insertNode(root, 4);
74+
insertNode(root, 10);
75+
insertNode(root, 2);
76+
insertNode(root, 6);
77+
insertNode(root, 9);
78+
insertNode(root, 12);
79+
std::cout << "Inorder traversal of tree:" << std::endl;
80+
inorder(root);
81+
std::cout << std::endl;
82+
83+
std::cout << "2nd smallest element in tree: " << kthSmallest(root, 2)
84+
<< std::endl;
85+
std::cout << "4th smallest element in tree: " << kthSmallest(root, 4)
86+
<< std::endl;
87+
std::cout << "7th smallest element in tree: " << kthSmallest(root, 7)
88+
<< std::endl;
89+
return 0;
90+
}
91+

0 commit comments

Comments
 (0)