首页 > 代码库 > [LeetCode] Populating Next Right Pointers in Each Node II
[LeetCode] Populating Next Right Pointers in Each Node II
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。