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

【C++】二叉搜索树的模拟实现和二叉树进阶OJ

二叉树OJ题

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

class Solution {
public:string tree2str(TreeNode* root) {if(root==nullptr){return "";}string str=to_string(root->val);if(root->left||root->right){str+='(';str+=tree2str(root->left);str+=')';}if(root->right){str+='(';str+=tree2str(root->right);str+=')';}return str;}
};

2.二叉树的最近公共祖先
法一:

class Solution {
public:bool Find(TreeNode* tree, TreeNode* x){if(tree==nullptr)return false;return tree==x||Find(tree->left,x)||Find(tree->right,x);}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root==nullptr)return nullptr;if(root==p||root==q){return root;}bool pInLeft,pInRight,qInLeft,qInRight;pInLeft=Find(root->left,p);pInRight=!pInLeft;qInLeft=Find(root->left,q);qInRight=!qInLeft;if(pInLeft&&qInLeft){return lowestCommonAncestor(root->left,p,q);}else if(pInRight&&qInRight){return lowestCommonAncestor(root->right,p,q);}else{return root;}}
};

法二:

class Solution 
{
public:bool FindPath(TreeNode* root, TreeNode* x,stack<TreeNode*>& Path){if(root==nullptr){return false;}Path.push(root);if(root==x)return true;if(FindPath(root->left,x,Path))return true;if(FindPath(root->right,x,Path))return true;Path.pop();return false;}TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {stack<TreeNode*> pPath,qPath;FindPath(root,p,pPath);FindPath(root,q,qPath);while(pPath.size()>qPath.size()){pPath.pop();}while(pPath.size()<qPath.size()){qPath.pop();}while(pPath.top()!=qPath.top()){pPath.pop();qPath.pop();}return pPath.top();}
};

3.二叉搜索树与双向链表

class Solution 
{
public:void InOrder(TreeNode* cur,TreeNode*& prev){if(cur==nullptr)return;InOrder(cur->left,prev);cur->left=prev;if(prev)prev->right=cur;prev=cur;InOrder(cur->right,prev);}TreeNode* Convert(TreeNode* pRootOfTree) {TreeNode* prev=nullptr;InOrder(pRootOfTree,prev);TreeNode* head=pRootOfTree;//pRootOfTree仍然是根节点,需要向左遍历找到链表头while(head&&head->left){head=head->left;}return head;}
};

4.从前序和中序遍历序列构造二叉树

class Solution {
public:TreeNode* _build(vector<int>& preorder, vector<int>& inorder,int& prei,int inbegin,int inend){if(inbegin>inend){return nullptr;}TreeNode* root=new TreeNode(preorder[prei]);int rooti=inbegin;while(rooti<=inend){if(preorder[prei]==inorder[rooti]){break;}++rooti;}//[inbegin,rooti-1] rooti [rooti+1,inend]++prei;root->left=_build(preorder,inorder,prei,inbegin,rooti-1);root->right=_build(preorder,inorder,prei,rooti+1,inend);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {int i=0;TreeNode* root=_build(preorder,inorder,i,0,inorder.size()-1);return root;}
};

5.非递归实现二叉树的前序遍历

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {stack<TreeNode*> st;TreeNode* cur=root;vector<int> v;while(cur||!st.empty()){//访问一棵树的开始//1.访问左路节点左路节点入栈,后续依次访问左路节点的右子树while(cur){v.push_back(cur->val);st.push(cur);cur=cur->left;}//依次访问左路节点的右子树TreeNode* top=st.top();st.pop();//子问题的方式访问右子树cur=top->right;}return v;}
};

6.非递归实现二叉树的中序遍历

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {stack<TreeNode*> st;vector<int> v;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;}
};

7.非递归实现二叉树的后序遍历

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {stack<TreeNode*> st;vector<int> v;TreeNode* prev=nullptr;TreeNode* cur=root;while(cur||!st.empty()){while(cur){st.push(cur);cur=cur->left;}TreeNode* top=st.top();if(top->right==nullptr||top->right==prev){prev=top;v.push_back(top->val);st.pop();}else{cur=top->right;}}return v;}
};

二叉搜索树的模拟实现

#pragma once
namespace Q
{template<class K>struct BSTreeNode{struct BSTreeNode<K>* _left;struct BSTreeNode<K>* _right;K _key;BSTreeNode(const K& key):_left(nullptr),_right(nullptr),_key(key){ }};template<class K>class BSTree{typedef BSTreeNode<K> Node;public:BSTree():_root(nullptr){ }BSTree(const BSTree<K>& t){_root = Copy(t._root);}BSTree<K>& operator=(BSTree<K> t){swap(_root, t._root);return *this;}~BSTree(){Destroy(_root);}bool Insert(const K& key){if (_root == nullptr){_root = new Node(key);return true;}Node* parent = nullptr;Node* cur = _root;while (cur){if (cur->_key < key){parent = cur;cur = cur->_right;}else if (cur->_key > key){parent = cur;cur = cur->_left;}else{return false;}}cur = new Node(key);if (parent->_key < key){parent->_right = cur;}else{parent->_left = cur;}return true;}bool find(const K& key){if (_root == nullptr){return false;}Node* cur = _root;while (cur){if (cur->_key < key){cur = cur->_right;}else if (cur->_key > key){cur = cur->_left;}else{return true;}}return false;}bool Erase(const K& key){if (_root == nullptr){return false;}Node* parent = nullptr;Node* cur = _root;while (cur){if (cur->_key < key){parent = cur;cur = cur->_right;}else if (cur->_key > key){parent = cur;cur = cur->_left;}else{if (cur->_left == nullptr){if (cur == _root){_root = cur->_right;}else{if (cur == parent->_left){parent->_left = cur->_right;}else{parent->_right = cur->_right;}}}else if (cur->_right == nullptr){if (cur == _root){_root = cur->_left;}else{if (cur == parent->_left){parent->_left = cur->_left;}else{parent->_right = cur->_left;}}}else{Node* parent = cur;Node* leftMax = cur->_left;while (leftMax->_right){parent = leftMax;leftMax = leftMax->_right;}swap(cur->_key, leftMax->_key);if (parent->_left == leftMax){parent->_left = leftMax->_left;}else{parent->_right = leftMax->_left;}cur = leftMax;}delete cur;cur = nullptr;return true;}}return false;}void InOrder(){_InOrder(_root);cout << endl;}bool FindR(const K& key){return _FindR(_root, key);}bool InsertR(const K& key){return _InsertR(_root, key);}bool EraseR(const K& key){return _EraseRdi(_root, key);}private:Node* Copy(Node* root){if (root == nullptr)return nullptr;Node* copyroot = new Node(root->_key);copyroot->_left = Copy(root->_left);copyroot->_right = Copy(root->_right);return copyroot;}void Destroy(Node*& root){if (root == nullptr)return;Destroy(root->_left);Destroy(root->_right);delete root;root = nullptr;}bool _EraseR(Node*& root, const K& key){if (root == nullptr)return false;if (root->_key < key){return _EraseR(root->_right, key);}else if (root->_key > key){return _EraseR(root->_left, key);}else{Node* del = root;if (root->_left == nullptr){root = root->_right;}else if (root->_right == nullptr){root = root->_left;}else//root就是已找到的现在要删除的节点。下面是它左右子树都存在的情况。{Node* leftMax = root->_left;while (leftMax->_right){leftMax = leftMax->_right;}swap(root->_key, leftMax->_key);return _EraseR(root->_left, key);}delete del;return true;}}bool _InsertR(Node*& root, const K& key){if (root == nullptr){root = new Node(key);return true;}if (root->_key < key){return _InsertR(root->_right, key);}else if (root->_key > key){return _InsertR(root->_left, key);}else{return false;}}bool _FindR(Node* root, const K& key){if (root == nullptr){return false;}if (root->_key < key){return _FindR(root->_right, key);}else if (root->_key > key){return _FindR(root->_left, key);}else{return true;}}void _InOrder(Node* root){if (root == nullptr){return;}_InOrder(root->_left);cout << root->_key << " ";_InOrder(root->_right);}Node* _root;};
}
http://www.dtcms.com/a/410788.html

相关文章:

  • Redis - Bitmap 类型
  • AUTOSAR 自适应平台 如何保证时间同步的可靠性?出现故障怎么办?
  • 设计互动网站建设做网页向网站提交数据
  • 北京架设网站杭州 建设网站制作
  • 学习笔记:Vue Router 编程式导航详解
  • Centos 7 创建ftp 权限最大支持上传删减
  • 哪家公司设计网站学用php做网站
  • 租用网站服务器网站改版 升级的目的是什么意思
  • java 项目docker 部署。
  • 【知识库文档】数据预处理PDF文档转成MD格式(gptpdf )
  • Java 高效实现 PowerPoint 转 PDF:不依赖Office
  • 新奇特:神经网络烘焙坊(下),万能配方的甜蜜奥义
  • 翁恺老师C语言基础教程代码学习
  • 天津建设网站的公司哪家好云南昆明网站建设价格
  • 网站开发需求书中山网络推广公司
  • RPC在分布式存储系统中的应用
  • 交互式手机网站网站建设功能
  • 07.容器监控
  • 学做网站哪里学郑州一网网站建设
  • react中redux的使用详细说明
  • 解码Android 系统蓝牙音频全流程
  • 做网站的文案wordpress 标签 修改
  • 疑难bug之正确处理InterruptedException
  • 【学习日记】[SSM]
  • 告别Print: Python调试入门,用PDB高效找Bug
  • 解决跨浏览器兼容性问题:CSS Flexbox布局在IE中的错位BUG
  • LeetCode 0611.有效三角形的个数:双指针
  • js 网站校验网络营销推广的优势
  • 好的响应式网站注册域名免费永久
  • 无人机图传模块——让飞行视界无界限