给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
中序遍历压入栈中
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: TreeNode* KthNode(TreeNode* pRoot, unsigned int k) { if(pRoot==NULL||k==0) return NULL; vector<TreeNode*> vec; Node(pRoot,vec); if(k>vec.size()) return NULL; return vec[k-1]; } void Node(TreeNode* pRoot, vector<TreeNode*>& vec) { if(pRoot==NULL)return ; if(pRoot->left!=NULL) Node(pRoot->left,vec); vec.push_back(pRoot); Node(pRoot->right,vec); } };