LeetCode 230: Kth Smallest Element in a BST to find the binary sorting tree, leetcodekth
Blog reprint please note address: http://blog.csdn.net/SunliyMonkey/article/details/48179133
Description
Address: https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Search for the k-th largest number in a binary sorting tree.
Site trap
None
Code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int ret; int aim; void search(TreeNode* x, int &ord){ if(!x || ord >= aim) return; search(x->left, ord); ord++; if(ord == aim){ ret = x->val; return; } search(x->right, ord); } int kthSmallest(TreeNode* root, int k) { int ord = 0; aim = k; search(root, ord); return ret; }};
Ranking
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Blog address: http://blog.csdn.net/sunliymonkey