当前位置: 首页 > news >正文

【C++刷题】二叉树进阶刷题

  1. 根据二叉树创建字符串

在这里插入图片描述

class Solution {
public:/** ()的省略有两种情况* 1.左右都为空,省略* 2.左子树不为空,右子树为空,省略*/string tree2str(TreeNode* root){string s;if(root == nullptr){return s;}s += to_string(root->val);if(root->left){s += "(";s += tree2str(root->left);s +=")";}else if(root->right) // 为了应对左为空,右不为空的情况{s += "()";}if(root->right){s += "(";s += tree2str(root->right);s += ")";}return s;}
};
  1. 二叉树的层序遍历
class Solution {
public:vector<vector<int>> levelOrder(TreeNode* root){vector<vector<int>> vv;queue<TreeNode*> q;int levelSize = 0;if(root == nullptr){return vv;}q.push(root);vector<int> v;while(!q.empty()){v.resize(0);levelSize = q.size();while(levelSize--){if((q.front())->left){q.push((q.front())->left);}if((q.front())->right){q.push((q.front())->right);}v.push_back((q.front())->val);q.pop();}vv.push_back(v);}return vv;}
};
  1. 二叉树的层序遍历 II

只需将正着层序遍历的结果reverse()一下即可。

  1. 二叉树的最近公共祖先

在这里插入图片描述
在这里插入图片描述

class Solution {
public:bool FindPath(TreeNode* root, TreeNode* x, stack<TreeNode*>& st){if(root == nullptr){return false;}st.push(root);if(root == x){return true;}if(FindPath(root->left, x, st)){return true;}if(FindPath(root->right, x, st)){return true;}st.pop();return false;}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){stack<TreeNode*> pPath, qPath;FindPath(root, p, pPath); // 找从根节点root到p的路径FindPath(root, q, qPath); // 找从根节点root到q的路径while(pPath.size() != qPath.size()){if(pPath.size() > qPath.size()){pPath.pop();} else{qPath.pop();}}while(pPath.top() != qPath.top()){pPath.pop();qPath.pop();}return pPath.top();}
};
  1. 二叉搜索树与双向链表

在这里插入图片描述
在这里插入图片描述

class Solution {
public:void InOrderConvert(TreeNode* cur, TreeNode*& prev){if(cur == nullptr){return;}InOrderConvert(cur->left, prev);// 双向链接cur->left = prev;if(prev){prev->right = cur;}// prev向后移prev = cur;InOrderConvert(cur->right, prev);}TreeNode* Convert(TreeNode* pRootOfTree){TreeNode* prev = nullptr;// 中序遍历进行链接InOrderConvert(pRootOfTree, prev);// 找头节点TreeNode* head = pRootOfTree;while(head && head->left){head = head->left;}return head;}
};
  1. 从前序与中序遍历序列构造二叉树

在这里插入图片描述

class Solution {
public:TreeNode* _buildTree(vector<int>& preorder, vector<int>& inorder, int& prei, int inbegin, int inend){// 子树区间确认是否继续递归创建子树if(inbegin > inend)return nullptr;// 前序创建树TreeNode* root = new TreeNode(preorder[prei++]);// 中序分割左右子树int ini = inbegin;while(ini <= inend){if(inorder[ini] == root->val)break;else++ini;}root->left = _buildTree(preorder, inorder, prei, inbegin, ini - 1);root->right = _buildTree(preorder, inorder, prei, ini + 1, inend);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder){int i = 0;return _buildTree(preorder, inorder, i, 0, inorder.size() - 1);}
};
  1. 从中序与后序遍历序列构造二叉树
class Solution {
public:TreeNode* _buildTree(vector<int>& inorder, vector<int>& postorder, int& posti, int inbegin, int inend){if(inbegin > inend)return nullptr;TreeNode* root = new TreeNode(postorder[posti--]);int ini = inbegin;while(ini <= inend){if(inorder[ini] == root->val)break;else++ini;}root->right = _buildTree(inorder, postorder, posti, ini + 1, inend);root->left = _buildTree(inorder, postorder, posti, inbegin, ini - 1);return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder){int i = postorder.size() - 1;return _buildTree(inorder, postorder, i, 0, inorder.size() - 1);}
};
http://www.lryc.cn/news/165434.html

相关文章:

  • 有效的数独
  • Vue导航守卫beforeRouteEnter,beforeRouteUpdate,beforeRouteLeave
  • 小红书《乡村振兴战略下传统村落文化旅游设计》中南大许少辉八一新著
  • Android13 下拉菜单栏中添加快捷截图按钮
  • GFS文件系统
  • 22 相交链表
  • 简历(快速上手)
  • wpf复制xaml及其cs窗体到其他项目 添加现有项,选 .xaml.cs,点添加即可。VS2022
  • 在线旅游平台步入新时代,携程如何走出自己的路?
  • java中feign远程调用底层是用Hystrix作为熔断器吗?
  • Web安全——穷举爆破下篇(仅供学习)
  • 强大的JTAG边界扫描(5):FPGA边界扫描应用
  • 苍穹外卖集成 Apache POI Java实现Excel文件的读写下载
  • iOS逆向:工具安装
  • 十种数据库缓存相关的技术和机制
  • 【C++】封装unordered_map和unordered_set(用哈希桶实现)
  • 面试问题回忆
  • 更多场景、更多选择,Milvus 新消息队列 NATS 了解一下
  • 如何通过python实现一个web自动化测试框架?
  • Linux —— 信号阻塞
  • 【【萌新编写riscV之计算机体系结构之CPU 总二】】
  • error:03000086:digital envelope routines::initialization error
  • 暴涨130万粉仅用3个月,一招转型成B站热门UP主
  • 【Linus】vim的使用:命令模式、底行模式、插入模式、视图模式、替换模式的常用操作介绍
  • leetcode第362场周赛补题
  • SpringMvc 之crud增删改查应用
  • 【业务功能109】微服务-springcloud-springboot-Skywalking-链路追踪-监控
  • 《向量数据库指南》——AI原生向量数据库Milvus Cloud 2.3架构升级
  • Flutter中实现交互式Webview的方法
  • 【Java Web】用Redis优化登陆模块