二叉搜索树第k个节点
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 }; 10 */ 11 class Solution { 12 public: 13 TreeNode* KthNode(TreeNode* pRoot, int k) 14 { 15 if(!pRoot) return nullptr; 16 vector<TreeNode*> res; 17 Sortnode(pRoot,res); 18 if(k<=0 || k>res.size()) return nullptr; 19 20 return res[k-1]; 21 } 22 void Sortnode(TreeNode* root,vector<TreeNode*>& vec){ 23 if(!root) return; 24 Sortnode(root->left,vec); 25 vec.push_back(root); 26 Sortnode(root->right,vec); 27 } 28 };