天河建设网站方案深圳龙岩技术科技有限公司
web/
2026/1/17 14:25:09/
文章来源:
天河建设网站方案,深圳龙岩技术科技有限公司,菜谱网站开发,襄阳网站seo公司这是关于一个普通双非本科大一学生的C的学习记录贴
在此前#xff0c;我学了一点点C语言还有简单的数据结构#xff0c;如果有小伙伴想和我一起学习的#xff0c;可以私信我交流分享学习资料
那么开启正题
今天分享的是关于二叉树的题目
1.从前序与中序遍历序列构造二叉…这是关于一个普通双非本科大一学生的C的学习记录贴
在此前我学了一点点C语言还有简单的数据结构如果有小伙伴想和我一起学习的可以私信我交流分享学习资料
那么开启正题
今天分享的是关于二叉树的题目
1.从前序与中序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树
给定两个整数数组 preorder 和 inorder 其中 preorder 是二叉树的先序遍历 inorder 是同一棵树的中序遍历请构造二叉树并返回其根节点。
这道题是很经典的二叉树题目原理就是利用前序插入数据中序判断左右子树构建二叉树利用了前序和中序的性质同时也利用了递归的思想
class Solution {
public:TreeNode* _buildTree(vectorintpreorder, vectorint inorder, int inbegin, int inend, int prei){//判断是否需要构建if(inbegin inend)return nullptr;//构建头结点TreeNode* root new TreeNode(preorder[prei]);//查找分隔点int rooti inbegin;while(rooti inend){if(inorder[rooti] preorder[prei])break;elserooti;}//如果有数据递归链接左右子树if(inbegin rooti - 1)root-left _buildTree(preorder, inorder, inbegin, rooti - 1, prei);if(rooti 1 inend)root-right _buildTree(preorder, inorder, rooti 1, inend, prei);return root;}TreeNode* buildTree(vectorint preorder, vectorint inorder) {int inbegin 0, inend inorder.size() - 1, prei 0;//原函数无法直接完成递归借助子函数完成TreeNode* ret _buildTree(preorder, inorder, inbegin, inend, prei);return ret;}
};
这是ac代码
2.从中序与后序遍历蓄力构造二叉树
106. 从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder 和 postorder 其中 inorder 是二叉树的中序遍历 postorder 是同一棵树的后序遍历请你构造并返回这颗 二叉树
这题与上面的题是姊妹题仅有细微的差别这里直接给代码
class Solution {
public:TreeNode* _buildTree(vectorint inorder, vectorint postorder, int inbegin, int inend, int posti){//判断是否需要构建if(inbegin inend)return nullptr;//创建头结点TreeNode* root new TreeNode(postorder[posti]);//查找分隔点int rooti inbegin;while(rooti inend){if(inorder[rooti] postorder[posti])break;elserooti;}//递归创建左右子树并链接if(rooti 1 inend)root-right _buildTree(inorder, postorder, rooti 1, inend, --posti);if(inbegin rooti - 1)root-left _buildTree(inorder, postorder, inbegin, rooti - 1, --posti);return root;}TreeNode* buildTree(vectorint inorder, vectorint postorder){ int inbegin 0, inend inorder.size() - 1, posti postorder.size() - 1;TreeNode* ret _buildTree(inorder, postorder, inbegin, inend, posti);return ret;}
};
这是ac代码
3.二叉树的前序遍历
144. 二叉树的前序遍历
给你二叉树的根节点 root 返回它节点值的 前序 遍历。
二叉树的三种遍历在前面的学习都已经很熟悉了这里我们着重要掌握的是非递归的方法也是以后面试的一个小难题
这里先给出简单的递归解决办法
class Solution {
public:void _preorderTraversal(TreeNode* root, vectorint v){if(root nullptr)return;v.push_back(root-val);_preorderTraversal(root-left, v);_preorderTraversal(root-right, v);}vectorint preorderTraversal(TreeNode* root) { vectorint v;_preorderTraversal(root, v);return v;}
};
那么非递归方法要如何求解呢首先我们要明白非递归方法是利用迭代来实现的但其原理还是根据递归的办法来解决的实际运用中递归由于要不停的创建函数栈帧有效率损耗实际上优化后时间差的并不多以及可能导致栈溢出所以我们才需要掌握非递归办法
非递归办法要创建一个stackpush进vector同时入栈在栈内模拟递归
class Solution {
public:vectorint preorderTraversal(TreeNode* root) {vectorint v;stackTreeNode* st;TreeNode* cur root;while(cur || !st.empty()){while(cur){st.push(cur);v.push_back(cur-val);cur cur-left;}TreeNode* top st.top();st.pop();cur top-right;}return v;}
};
这是ac代码
4.二叉树的中序遍历
94. 二叉树的中序遍历
和前序遍历如出一辙这里直接给出递归代码以及非递归代码
a.递归方法
class Solution {
public:void _inorderTraversal(TreeNode* root, vectorint v) {if(root nullptr)return;_inorderTraversal(root-left, v);v.push_back(root-val);_inorderTraversal(root-right, v);}vectorint inorderTraversal(TreeNode* root) {vectorint v;_inorderTraversal(root, v);return v;}
};
b.非递归方法
class Solution {
public:vectorint inorderTraversal(TreeNode* root) {vectorint v;stackTreeNode* st;TreeNode* cur root;while(cur || !st.empty()){while(cur){st.push(cur);cur cur-left;}TreeNode* top st.top();st.pop();v.push_back(top-val);cur top-right;}return v;}
};5.二叉树的后序遍历
和上面的题相似但是后序遍历要更复杂一点因为要最后读取根所以我们要记录读取的值判断是否遍历完了子树再对根进行读取
class Solution {
public:void _postorderTraversal(TreeNode* root, vectorint v){if(!root)return;_postorderTraversal(root-left, v);_postorderTraversal(root-right, v);v.push_back(root-val);}vectorint postorderTraversal(TreeNode* root) {vectorint v;_postorderTraversal(root, v);return v;}
};
class Solution {
public:vectorint postorderTraversal(TreeNode* root) {vectorint v;stackTreeNode* st;TreeNode* cur root;TreeNode* last nullptr;while(cur || !st.empty()){while(cur){st.push(cur);cur cur-left;}TreeNode* top st.top();if(top-right nullptr || last top-right){st.pop();last top;v.push_back(top-val);}else{cur top-right;}} return v;}
};
这是ac代码
新手写博客有不对的位置希望大佬们能够指出也谢谢大家能看到这里让我们一起学习进步吧
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/87757.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!