Leetcode: Populating Next Right Pointers in Each Node II, leetcodepopulating

Source: Internet
Author: User

Leetcode: Populating Next Right Pointers in Each Node II, leetcodepopulating

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    7After calling your function, the tree should look like:         1 -> NULL       /  \      2 -> 3 -> NULL     / \    \    4-> 5 -> 7 -> NULL

Based on the Populating Next Right Pointers in Each Node problem, the difficulty is 20 and the method is the same. They are similar to Binary Tree Level Order Traverse. They all regard the Tree as an undirected graph. Then, in the BFS mode, we need to record the ParentNumInQueue and ChildNumInQueue of each layer. The initial values are 1 and 0, in the future, if ParentNumInQ is reduced to 0, this layer has been traversed, and the number of children in this layer will become the ParentNumInQ of the next layer.

 1 /** 2  * Definition for binary tree with next pointer. 3  * public class TreeLinkNode { 4  *     int val; 5  *     TreeLinkNode left, right, next; 6  *     TreeLinkNode(int x) { val = x; } 7  * } 8  */ 9 public class Solution {10     public void connect(TreeLinkNode root) {11         if (root == null) return;12         LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();13         queue.add(root);14         int ParentNumInQ = 1;15         int ChildNumInQ = 0;16         TreeLinkNode pre = null;17         while (!queue.isEmpty()) {18             TreeLinkNode cur = queue.poll();19             ParentNumInQ--;20             if (pre == null) {21                 pre = cur;22             }23             else {24                 pre.next = cur;25                 pre = pre.next;26             }27             if (cur.left != null) {28                 queue.add(cur.left);29                 ChildNumInQ++;30             }31             if (cur.right != null) {32                 queue.add(cur.right);33                 ChildNumInQ++;34             }35             if (ParentNumInQ == 0) {36                 ParentNumInQ = ChildNumInQ;37                 ChildNumInQ = 0;38                 pre.next = null;39                 pre = null;40             }41         }42     }43 }

 




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.