最后没调出来,闲下来时候调了一下,感觉重视基础很重要,字符串的输入转int类型,如何初始化二叉树,然后中序遍历即可,哎,一道没A,记录一下吧 #include<iostream> #include<vector> #include<string> #include<sstream> #include<queue> #include<cstring> using namespace std; struct TreeNode { int val; TreeNode* l; TreeNode* r; TreeNode(int x): val(x),l(NULL),r(NULL){ } }; TreeNode* insert(vector<int> vec,int size){ TreeNode** nodes = new TreeNode*[size]; for(int i=0;i<size;i++){ if(vec[i]==0){ nodes[i]=NULL; }else{ nodes[i]=new TreeNode(vec[i]); } } queue<TreeNode*> nodeQ; nodeQ.push(nodes[0]); TreeNode*node; int index = 1; while(index<size){ node = nodeQ.front(); nodeQ.pop(); nodeQ.push(nodes[index++]); node->l= nodeQ.back(); nodeQ.push(nodes[index++]); node->r= nodeQ.back(); } return nodes[0]; } void print_inorder(TreeNode* node,vector<int> &s){ if(!(node)) return ; print_inorder(node->l,s); s.push_back(node->val); print_inorder(node->r,s); return; } int main(){ string str; TreeNode* node = nullptr; vector<int>vec2; // while(getline(cin,str)){ cin>>str; stringstream ss(str); vector<int>vec; string token; int str2; cin>>str2; while(getline(ss,token,',')){ vec.push_back(stoi(token)); } node = insert(vec,vec.size()); print_inorder(node,vec2); for(int i=0;i<vec2.size()-1;i++){ if(vec2[i]==str2){ cout<<vec2[i+1]; } } return 0; }
点赞 评论

相关推荐

2025-12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
2025-12-17 17:15
华东师范大学 运营
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务