117. Populating Next Right Pointers in Each Node II**
来源:互联网 发布:阿里云服务器1核2g 编辑:程序博客网 时间:2024/05/16 05:17
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULLMy code:
public class Solution { public void connect(TreeLinkNode root) { TreeLinkNode level_start = root; while(level_start!=null){ TreeLinkNode cur = level_start; while(cur!=null){ TreeLinkNode temp = cur.next; while(temp!=null){ if(temp.left!=null) { temp=temp.left; break; } else if(temp.right!=null) { temp=temp.right; break; } else temp=temp.next; } if(cur.right!=null){ cur.right.next= temp; if(cur.left!=null){ cur.left.next=cur.right; } } else{ if(cur.left!=null) cur.left.next=temp; } cur=cur.next; } while(level_start!=null){ if(level_start.left!=null) { level_start=level_start.left; break; } else if(level_start.right!=null){ level_start=level_start.right; break; } else level_start=level_start.next; } } }}总结:层次遍历
public class Solution { public void connect(TreeLinkNode root) { TreeLinkNode head = null; //head of the next level TreeLinkNode prev = null; //the leading node on the next level TreeLinkNode cur = root; //current node of current level while (cur != null) { while (cur != null) { //iterate on the current level //left child if (cur.left != null) { if (prev != null) { prev.next = cur.left; } else { head = cur.left; } prev = cur.left; } //right child if (cur.right != null) { if (prev != null) { prev.next = cur.right; } else { head = cur.right; } prev = cur.right; } //move to next node cur = cur.next; } //move to next level cur = head; head = null; prev = null; } }}总结:省去了while循环,把对当前cur的搜索,改为对cur的child的搜索。
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
- 电信诈骗之更改号码显示
- 【密码学】一万字带您走进密码学的世界(下)
- BZOJ 4247(挂饰-背包)
- 【Android View绘制之旅】Measure过程
- linux 安装 迅雷
- 117. Populating Next Right Pointers in Each Node II**
- 小光棍数
- python 简单爬虫下载图片
- 关于Centos7的yum更新
- 【小云工作室】我的第一个开源项目
- 洛谷 1604——B进制星球(高精度算法)
- 小米Ubuntu使用独立显卡
- leecode 解题总结:15 3Sum
- IMBD影评数据的情感分析