Populating Next Right Pointers in Each Node
2013-04-20 15:23
260 查看
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public void connect(TreeLinkNode root) { // Start typing your Java solution below // DO NOT write main() function if(root==null)return; if(root!=null&&root.left==null) {root.next=null;return;} Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>(); q.add(root.left); q.add(root.right); root.next=null; int num; while(!q.isEmpty()){ num = q.size()-1; TreeLinkNode pre = q.remove(); if(pre.left!=null){ q.add(pre.left); q.add(pre.right);} while(num>0){ TreeLinkNode latter = q.remove(); if(latter.left!=null){ q.add(latter.left); q.add(latter.right); } pre.next = latter;pre = latter;num--; } pre.next=null; } } }
相关文章推荐
- LeetCode - Populating Next Right Pointers in Each Node
- leetcode[117]Populating Next Right Pointers in Each Node II
- [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
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode OJ:Populating Next Right Pointers in Each Node
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
- LEETCODE —— Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- 【Leetcode】Populating Next Right Pointers in Each Node
- leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node 深度搜索
- 116. Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node
- [LeetCode][JavaScript]Populating Next Right Pointers in Each Node