某公司组织架构以二叉搜索树形式记录,节点值为处于该职位的员工编号。请返回第 cnt 大的员工编号。

示例 1:

**输入:**root = [7, 3, 9, 1, 5], cnt = 2
7
/ \
3 9
/ \
1 5
**输出:**7

示例 2:

输入: root = [10, 5, 15, 2, 7, null, 20, 1, null, 6, 8], cnt = 4
10
/ \
5 15
/ \ \
2 7 20
/ / \
1 6 8
输出: 8

提示:

1 ≤ cnt ≤ 二叉搜索树元素个数

二叉搜索树中序遍历,按左子树,根节点,右子树是从小到大

先右子树即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
vector<int> m;
int findTargetNode(TreeNode *root, int cnt) {
findCnt(root, cnt);
return m[cnt - 1];
}
void findCnt(TreeNode *root, int cnt) {
if (root == nullptr || cnt == 0)
return;
findCnt(root->right, cnt);
m.push_back(root->val);
findCnt(root->left, cnt - 1);
}
};