From b590c830a469e9ee9d19f049299ea6e12ac97017 Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Sat, 2 Nov 2024 17:44:40 +0100 Subject: [PATCH 1/2] added solution for 230. Kth Smallest Element in a BST --- C++/kth-smallest-element-in-a-bst.cpp | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 C++/kth-smallest-element-in-a-bst.cpp diff --git a/C++/kth-smallest-element-in-a-bst.cpp b/C++/kth-smallest-element-in-a-bst.cpp new file mode 100644 index 00000000..b327a36c --- /dev/null +++ b/C++/kth-smallest-element-in-a-bst.cpp @@ -0,0 +1,35 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int kthSmallest(TreeNode* root, int k) { + int count = 0; + return inorder(root, k, count); + } + +private: + int inorder(TreeNode* node, int k, int& count) { + //Base case: Zero node + if(!node) return -1; + + //Traverse left branch for kth smallest value: + int leftVal = inorder(node->left, k, count); + if(count == k) return leftVal; + + //Process root node: + ++count; + if(count == k) return node->val; + + //Traverse right branch for kth smallest value: + return inorder(node->right, k, count); + } +}; \ No newline at end of file From 0b41db634d2357c569354e483cfbf5e1e5a5404c Mon Sep 17 00:00:00 2001 From: n1i9c9k9 Date: Sat, 2 Nov 2024 18:10:59 +0100 Subject: [PATCH 2/2] added solution for 230. Kth Smallest Element in a BST + updated README + Table Entry under DFS --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b2cac739..e2353053 100644 --- a/README.md +++ b/README.md @@ -359,6 +359,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Java](./Java/Balanced-Binary-Tree.java) | _O(n)_ | _O(n)_ | Easy | DFS | | | 1376 | [ Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/) | [C++](./C++/Cherry-Pickup-II.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | | | 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [C++](./C++/number-of-islands.cpp) | _O(m * n)_ | _O(m * n)_ | Medium | DFS | | +| 230 | [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [C++](./C++/kth-smallest-element-in-a-bst.cpp) | _O(n)_ | _O(n)_ | Medium | DFS | |
@@ -515,6 +516,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Nick H](https://github.com/n1i9c9k9)
| Germany | C++ | [GitHub](https://github.com/n1i9c9k9)
⬆️ Back to Top