Leetcode 117. Populating Next Right Pointers in Each Node II
来源:互联网 发布:锐思数据库怎么用 编辑:程序博客网 时间:2024/06/05 10:50
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 -> NULL
Subscribe to see which companies asked this question
public class Solution { private Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>(); public void connect(TreeLinkNode root) { if(root == null) return ; q.offer(root); while(!q.isEmpty()){ TreeLinkNode node = q.poll(); if(node.left !=null){ q.offer(node.left); } if(node.right !=null){ q.offer(node.right); } TreeLinkNode leftNode = node.left; TreeLinkNode rightNode = node.right; TreeLinkNode oneChild = null; if(leftNode!=null && rightNode!=null){ leftNode.next = rightNode; oneChild = rightNode; } else if(leftNode!=null){ oneChild = leftNode; } else if(rightNode!=null){ oneChild = rightNode; } TreeLinkNode tmp = node.next; while( oneChild!=null && tmp!=null){ if(tmp.left!=null){ oneChild.next = tmp.left; break; } else if(tmp.right!=null){ oneChild.next = tmp.right; break; } tmp = tmp.next; } } }}
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
- [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
- [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
- [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
- 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
- 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
- POI初体验及如何导出百万数据
- MySQl数据库必会sql语句加强版
- Android获取读取内部存储-初级
- Android 4.4 图形架构
- c++string类的常用方法详解
- Leetcode 117. Populating Next Right Pointers in Each Node II
- ACM--DP--HDOJ 1005--Number Sequence
- ListView中点选RadioButton的解决方案
- maven项目多源文件夹编译、打包设置
- 邮箱处理
- Perl中的正则
- ST意法半导体官方库启动文件 startup_stm32f10x_hd.s的分析
- GOF23种设计模式精解
- Linux mint 18编译CUDA 7.5 Sample