标签:bsp color title tle 二叉搜索树 class int res log
/* 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, int k) { if(!pRoot || k <= 0) return NULL; stack<TreeNode *> stk; TreeNode *res = NULL; TreeNode *p = pRoot; int index = 0; while(p || !stk.empty()){ while(p){ stk.push(p); p = p->left; } if(!stk.empty()){ p = stk.top(); stk.pop(); index++; if(index == k){ res = p; break; } p = p->right; } } return res; } };
标签:bsp color title tle 二叉搜索树 class int res log
原文地址:http://www.cnblogs.com/xiuxiu55/p/6751085.html