剑指offer 面试题63:二叉搜索树的第k个结点(二叉树中序遍历的应用) 题解
2016-05-06 08:04
796 查看
面试题 63:二叉搜索树的第k个结点
题目:给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 (见下面的图1) 中,按结点数值大小顺序第三个结点的值为4。
图1:一个有7个结点的二叉搜索树,如果按结点数值大小顺序输出,则第3个结点的值是4
提交网址: http://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&tqId=11215
分析:
对于二叉搜索树BST,在树中任取一棵子树,其节点值都满足:左结点的值 < 父节点的值 < 右结点的值,故如果按照中序遍历的顺序遍历一棵二叉搜索树BST,遍历序列的数值是递增序的。只需要用中序遍历算法遍历一棵二叉搜索树BST,就可以找出它的第k大结点。非递归中序遍历加上计数器即可解决。
非递归实现 AC代码:
而提交同样作用的代码到牛客网OJ却报错了: control may reach end of non-void function [-Werror,-Wreturn-type, 本地在Visual Studio和Dev C++上都测试通过的...
意思好像是没有在函数最外层写统一的返回值,改了就AC了...
题目:给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 (见下面的图1) 中,按结点数值大小顺序第三个结点的值为4。
图1:一个有7个结点的二叉搜索树,如果按结点数值大小顺序输出,则第3个结点的值是4
提交网址: http://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&tqId=11215
分析:
对于二叉搜索树BST,在树中任取一棵子树,其节点值都满足:左结点的值 < 父节点的值 < 右结点的值,故如果按照中序遍历的顺序遍历一棵二叉搜索树BST,遍历序列的数值是递增序的。只需要用中序遍历算法遍历一棵二叉搜索树BST,就可以找出它的第k大结点。非递归中序遍历加上计数器即可解决。
6 / \ 3 8 / \ / \ 2 5 7 9
非递归实现 AC代码:
#include<cstdio> #include<vector> #include<stack> using namespace std; 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) { TreeNode *p=pRoot; TreeNode *resNode; if(p==NULL || k==0) return NULL; stack<TreeNode *> st; unsigned int count=0; while(!st.empty() || p != NULL) { if(p != NULL) { st.push(p); p=p->left; } if(p == NULL) { p=st.top(); // 取当前节点 count++; if(count==k) resNode=p; st.pop(); p=p->right; } } return resNode; } }; // 以下为测试 int main() { Solution sol; TreeNode* root = new TreeNode(6); root->left = new TreeNode(3); root->left->left = new TreeNode(2); root->left->right = new TreeNode(5); root->right = new TreeNode(8); root->right->left = new TreeNode(7); root->right->right = new TreeNode(9); TreeNode* p=sol.KthNode(root, 3); printf("The value of Kth Node is: %d\n", p->val); return 0; }
而提交同样作用的代码到牛客网OJ却报错了: control may reach end of non-void function [-Werror,-Wreturn-type, 本地在Visual Studio和Dev C++上都测试通过的...
意思好像是没有在函数最外层写统一的返回值,改了就AC了...
class Solution { public: TreeNode* KthNode(TreeNode* pRoot, unsigned int k) { TreeNode *p=pRoot; if(p==NULL || k==0) return NULL; stack<TreeNode *> st; unsigned int count=0; while(!st.empty() || p != NULL) { if(p != NULL) { st.push(p); p=p->left; } if(p == NULL) { p=st.top(); // 取当前节点 count++; if(count==k) return p; st.pop(); p=p->right; } } } };
相关文章推荐
- 职场怪物品鉴录——北漂18年(54)
- 个人项目面试题
- 腾讯面试题(附答案)
- 【随笔】值得纪念的一天,重新出发,从心出发
- 10个值得深思的 PHP 面试问题
- 蜂窝教育Java培训:你不仅仅是Java程序员!
- 当用户提出需要一匹千里马时,追问一句为什么
- 聊聊职场规划那些事儿
- 一个合格的程序员应该读过哪些书
- 广州宇信易诚科技有限公司面试题
- 狸猫的面试——JVM内存回收
- 读《程序员的自我修养 —— 静态链接》乱摘
- java面试题
- string函数的面试题分析
- 《java入门第一季》之面向对象(一个易错面试题)
- 《java入门第一季》之面向对象(一个易错面试题)
- [置顶] Android开发工程师面试题之handler详解。android程序员,android开发面试资料,详解
- 个人面试总结
- 集合栈之程序员面试经典
- 职业思考