116. Populating Next Right Pointers in Each Node
Posted 阿怪123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了116. Populating Next Right Pointers in Each Node相关的知识,希望对你有一定的参考价值。
/** * 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) { //依然采用深度优先的思想 Queue<TreeLinkNode> q=new LinkedList<TreeLinkNode>(); if(root==null) return ; q.offer(root); int size=q.size(); int count=0; TreeLinkNode temp=null; TreeLinkNode tail=null; while(!q.isEmpty()) { temp=q.poll(); if(temp.left!=null) q.offer(temp.left); if(temp.right!=null) q.offer(temp.right); if(count!=0) { tail.next=temp; } count++; if(count==size) { temp.next=null; count=0; size=q.size(); } tail=temp; } } }
以上是关于116. Populating Next Right Pointers in Each Node的主要内容,如果未能解决你的问题,请参考以下文章
116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each Node
116. Populating Next Right Pointers in Each Node