码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Populating Next Right Pointers in Each Node II

时间:2014-09-10 21:06:51      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   art   div   sp   log   

 1 public class Solution {
 2     public void connect(TreeLinkNode root) {
 3         TreeLinkNode upperTravelingNode = root;
 4         if (root==null) return;
 5         TreeLinkNode nowLevelStartNode = null;
 6         TreeLinkNode nowLevelTravelingNode = null;
 7         
 8         do {
 9             while (upperTravelingNode != null) {
10                 if (upperTravelingNode.left!=null) {
11                     if (nowLevelStartNode==null) {
12                         nowLevelStartNode=upperTravelingNode.left;
13                     } else {
14                         nowLevelTravelingNode.next = upperTravelingNode.left;
15                     }
16                     nowLevelTravelingNode = upperTravelingNode.left;
17                 }
18                 if (upperTravelingNode.right!=null) {
19                     if (nowLevelStartNode==null) {
20                         nowLevelStartNode=upperTravelingNode.right;
21                     } else {
22                         nowLevelTravelingNode.next = upperTravelingNode.right;
23                     }
24                     nowLevelTravelingNode = upperTravelingNode.right;                    
25                 }
26                 upperTravelingNode = upperTravelingNode.next;
27             }
28             upperTravelingNode = nowLevelStartNode;
29             nowLevelStartNode = null;
30         } while (upperTravelingNode != null);
31     }
32 }

 

[LeetCode] Populating Next Right Pointers in Each Node II

标签:style   blog   color   io   ar   art   div   sp   log   

原文地址:http://www.cnblogs.com/yuhaos/p/3965133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!