Created
December 26, 2017 01:36
-
-
Save sourabh2k15/a25308e4dbfd0f91f1164aea1f63598f to your computer and use it in GitHub Desktop.
Leetcode #230 Find Kth Smallest element in BST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int kthSmallest(TreeNode* root, int k) { | |
stack<TreeNode*> s; | |
while(root != NULL || !s.empty()){ | |
while(root != NULL){ | |
s.push(root); | |
root = root->left; | |
} | |
root = s.top(); s.pop(); | |
if(--k == 0) return root->val; | |
root = root->right; | |
} | |
return -1; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment