leetcode:Populating Next Right Pointers in Each Node
来源:互联网 发布:日韩护肤品推荐 知乎 编辑:程序博客网 时间:2024/06/06 06:23
/** * https://leetcode.com/problems/populating-next-right-pointers-in-each-node/#/description * */#include<iostream>#include<vector>#include<stack>using namespace std;using vecIter = std::vector<int>::iterator;struct TreeLinkNode { int val; TreeLinkNode *left, *right, *next; TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}};class Solution{public: Solution(){}; ~ Solution(){}; void connect(TreeLinkNode* root){ if(NULL==root) return; TreeLinkNode* curLev; while(root->left!=NULL){ curLev=root; while(curLev!=NULL){ curLev->left->next=curLev->right; if(curLev->next!=NULL) curLev->right->next=curLev->next->left; curLev=curLev->next; } root=root->left; } }};// Test Unit//// 1// / \// 2 3// /\ /\// 4 5 6 7// /\ /\ /\ /\// 8 9A BC DE F// ( A == 10 ...)TreeLinkNode* createBinaryTree(vecIter beg,vecIter end){ vector<TreeLinkNode*> vec; //把vector数组的int型内容,转化为树结构类型,存到vec数组当中 for(vecIter it=beg;it!=end;++it) vec.push_back(new TreeLinkNode(*it)); //把vec数组中的结果组织成树的形式 for(int i=0,pos=0;pos!=vec.size()-1;++i) { vec[i]->left=vec[++pos]; vec[i]->right=vec[++pos]; } //返回起始地址 return *vec.begin();}//输出节点的内容void print(TreeLinkNode* root){ while(root) { cout<<root->val; TreeLinkNode* cur=root->next; while(cur) { cout<<"->"<<cur->val; cur=cur->next; } cout<<endl; root=root->left; }}int main(int argc,char** argv){ vector<int> vec={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; TreeLinkNode* root=createBinaryTree(vec.begin(),vec.end()); Solution s; s.connect(root); print(root); return 0;}
阅读全文
0 0
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node
- mac下crontab执行定时脚本
- QT 制作开机动画
- 关于servlet的理解和总结
- useful url
- leetcode:Binary Tree Level Order Traversal 二叉树层序遍历
- leetcode:Populating Next Right Pointers in Each Node
- 解决UITableViewCell重用导致的重叠(字变粗)
- Java遇到异常
- 【python笔记】使用matplotlib,pylab进行python绘图
- 《算法导论》--归并排序
- 使用Nginx实现反向代理
- SwustOj A simple problem(0093)
- 大数据统计租房市场现状(北京篇)
- javascript创建选项卡----慕课10-1编程挑战