1. 주제
이진 검색 트리 감안할 때, 거기에 최초의 K 작은 노드를 찾아주십시오. 예를 들어, (5,3,7,2,4,6,8), 제 접속점에 따른 수치 값은 4 요약이다.
출처 : 안전 제공 입증
링크 : https://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&tqId=11215&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
2. 내 문제 솔루션
2.1 전순
- 어레이를 이송하기 위해 이진 검색 트리 오름차순 (트리 노드 값)이다]가 우선 탐색 순서 인
k
번호가 처음k
소수;
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
int cnt=0;
TreeNode * res=NULL;
void inOrder(TreeNode *root,int k){
if(!root)return;
inOrder(root->left,k);
if(++cnt == k){
res=root;
return;
}
inOrder(root->right,k);
}
public:
TreeNode* KthNode(TreeNode* pRoot, int k)
{
cnt=0;
inOrder(pRoot,k);
return res;
}
};