news 2026/8/27 9:31:25

二叉树其他题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
二叉树其他题

106. 从中序与后序遍历序列构造二叉树106. 从中序与后序遍历序列构造二叉树

​ ​ class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if(inorder.empty()||postorder.empty())return NULL; TreeNode*root=new TreeNode(postorder.back()); auto it=find(inorder.begin(),inorder.end(),root->val); int index=it-inorder.begin(); vector<int>leftin(inorder.begin(),inorder.begin()+index); vector<int>rightin(inorder.begin()+index+1,inorder.end()); vector<int>leftpost(postorder.begin(),postorder.begin()+index); vector<int>rightpost(postorder.begin()+index,postorder.end()-1); TreeNode*left=buildTree(leftin,leftpost); TreeNode*right=buildTree(rightin,rightpost); root->left=left; root->right=right; return root; } }; ​ ​

98. 验证二叉搜索树(遍历+中序 二叉搜索树具有中序性质)

class Solution { public: long long pre=LLONG_MIN; bool isValidBST(TreeNode* root) { if(root==NULL)return true; if(!isValidBST(root->left))return false; if(pre>=root->val){ return false; } else pre=root->val; return isValidBST(root->right); } };

530. 二叉搜索树的最小绝对差

class Solution { public: int result=INT_MAX; TreeNode*pre=NULL; void tra(TreeNode*root){ if(root==NULL)return; tra(root->left); if(pre!=NULL){ int min=root->val-pre->val; if(result>min)result=min; } pre=root; tra(root->right); return; } int getMinimumDifference(TreeNode* root) { tra(root); return result; } };

501. 二叉搜索树中的众数

class Solution { public: int index=0; TreeNode*pre=NULL; int now=1; vector<int>result; void tra(TreeNode* root){ if (root==NULL)return ; tra(root->left); if (pre == NULL || pre->val != root->val) { now = 1; } else { now++; } if(now==index){ result.push_back(root->val); } if(now>index){ result.clear(); result.push_back(root->val); index=now; } pre=root; tra(root->right); } vector<int> findMode(TreeNode* root) { tra(root); return result; } };

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

class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==NULL)return NULL; if(root==p||root==q)return root; TreeNode*left=lowestCommonAncestor(root->left,p,q); TreeNode*right=lowestCommonAncestor(root->right,p,q); if(left!=NULL&&right!=NULL)return root; else if(left!=NULL)return left; return right; } };

450.删除二叉搜索树中的节点

class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { if(root==NULL)return nullptr; if(root->val>key) root->left=deleteNode(root->left,key); else if(root->val<key)root->right=deleteNode(root->right,key); else{ TreeNode*tmp=root->right; if(tmp!=NULL){ while(tmp->left!=nullptr){ tmp=tmp->left; } tmp->left=root->left; root=root->right;} else root=root->left; } return root; } };

108. 将有序数组转换为二叉搜索树

class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(nums.empty())return NULL; int min=nums.size()/2; TreeNode*root=new TreeNode(nums[min]); vector<int>left1(nums.begin(),nums.begin()+min); vector<int>right1(nums.begin()+min+1,nums.end()); TreeNode*left=sortedArrayToBST(left1); TreeNode*right=sortedArrayToBST(right1); root->left=left; root->right=right; return root; } };
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/27 9:25:31

数字电源控制器选型:dsPIC33EP GS系列DSC核心外设与实战详解

做数字电源的工程师应该都有同感&#xff1a;模拟环路调起来确实快&#xff0c;一个运放加几个阻容网络就能把环路稳态调出来&#xff0c;可一旦客户要求多路输出、动态调压、PMBus通信、故障记录、在线升级&#xff0c;模拟方案就变成了一堆运放和电位器拼起来的"意大利面…

作者头像 李华
网站建设 2026/8/27 9:25:12

SPADE框架:让AI在自建代码环境中自对弈进化,提升大模型推理能力

先花三分钟理解一个现象&#xff1a;在过去两年的大模型训练里&#xff0c;数据几乎都是“人工标注 → 清洗 → 喂给模型”的单向流程。模型学完一批数据&#xff0c;能力提升&#xff0c;但下一次训练又要等新的数据被标注出来。这个过程在数学推理、代码生成、工具调用这类需…

作者头像 李华
网站建设 2026/8/27 9:23:24

Lodash.js实战价值:兼容性、可预测性与架构级能力

1. Lodash.js不是“万能胶”&#xff0c;而是JavaScript开发者的精密扳手你有没有遇到过这样的场景&#xff1a;写一个数组去重&#xff0c;先查MDN确认Set兼容性&#xff0c;再翻Babel配置看是否要转译&#xff1b;处理嵌套对象时&#xff0c;obj && obj.user &&a…

作者头像 李华
网站建设 2026/8/27 9:21:09

小米玄戒芯片技术沟通会前瞻:自研SoC如何影响手机体验与开发者生态

每一次手机芯片发布会&#xff0c;热闹的往往都是跑分和参数&#xff0c;但真正值得开发者与数码爱好者关注的&#xff0c;是芯片背后的技术取舍和产品定义思路。小米玄戒芯片技术沟通会选择在新机发布前单独召开&#xff0c;本身就释放了一个信号&#xff1a;这不仅仅是一次“…

作者头像 李华