117. Populating Next Right Pointers in Each Node II
来源:互联网 发布:淘宝钻石展位怎么样 编辑:程序博客网 时间:2024/06/16 07:00
本来想用DFS实现最后,但是发现了一些我不能解决的测试样例,还是采用了层序遍历这种具有普适性的思路。
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */class Solution {public: void connect(TreeLinkNode *root) { if(root==NULL) return; queue<TreeLinkNode *> node; queue<int> depth; TreeLinkNode * nowNode=NULL; int nowDepth=0; node.push(root); depth.push(1); while(!node.empty()) { if(node.front()->left!=NULL) { node.push(node.front()->left); depth.push(depth.front()+1); } if(node.front()->right!=NULL) { node.push(node.front()->right); depth.push(depth.front()+1); } if(nowDepth==depth.front()) nowNode->next=node.front(); nowNode=node.front(); nowDepth=depth.front(); node.pop(); depth.pop(); } }};
0 0
- LeetCode 117. Populating Next Right Pointers in Each Node II
- [LeetCode]117.Populating Next Right Pointers in Each Node II
- [Leetcode] 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- [leetcode] 117.Populating Next Right Pointers in Each Node II
- 116. Populating Next Right Pointers in Each Node && 117. II
- [LeetCode]117. Populating Next Right Pointers in Each Node II
- Leetcode 117. Populating Next Right Pointers in Each Node II
- [leetcode]117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- LeetCode 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- Leetcode 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- [LeetCode] 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- 117. Populating Next Right Pointers in Each Node II
- Visio2007安装
- 青蛙跳台阶问题
- 正则表达式,字符串操作
- Unity3D安卓平台发布配置
- JVM学习之类加载
- 117. Populating Next Right Pointers in Each Node II
- 新概念英语4 The sporting spirit 体育精神
- Android实现Activity之间通信的方法
- 【安卓逆向】如何用ida动态调试so文件
- Android 源码解析
- spring事务转账案例
- 从 Qt 的 delete 说开来
- 四、关于自定义view的文章:折线图进阶(触摸版)
- Delta-wave