时间空间都 满足,缺陷是使用了全局变量。恳请各位大佬指点。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: TreeNode* res; int n = 0; TreeNode* KthNode(TreeNode* pRoot, int k) { SearchNode(pRoot, k); return res; } void SearchNode(TreeNode* pRoot, int k){ if(pRoot == NULL){ return; } KthNode(pRoot->left, k); n++; if(n == k){ res = pRoot; } KthNode(pRoot->right, k); } };