Leetcode: populating next right pointers in each node

Source: Internet
Author: User
Given a binary tree    struct TreeLinkNode {      TreeLinkNode *left;      TreeLinkNode *right;      TreeLinkNode *next;    }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.Note:You may only use constant extra space.You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).For example,Given the following perfect binary tree,         1       /        2    3     / \  /     4  5  6  7After calling your function, the tree should look like:         1 -> NULL       /        2 -> 3 -> NULL     / \  /     4->5->6->7 -> NULL

Difficulty: 86. Similar to the binary tree level order traversal problem, the node at each layer is connected to a linked list, and the last node is connected to null. you need to find the first node of each layer and connect it back from it. This is the basic operation of the linked list. I used a treelinknode pre to do this front node. At first, I did not assign a value to it. During the traversal process, I recorded the previous hop node. At the end of each layer traversal, I set it to null. Determines whether the current node is the start of a layer based on whether it has been replicated.

 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         18         while (!queue.isEmpty()) {19             TreeLinkNode cur = queue.poll();20             if (pre == null) {21                 pre = cur;22             }23             else {24                 pre.next = cur;25                 pre = pre.next;26             }27             ParentNumInQ--;28             if (cur.left != null) {29                 queue.add(cur.left);30                 ChildNumInQ++;31             }32             if (cur.right != null) {33                 queue.add(cur.right);34                 ChildNumInQ++;35             }36             if (ParentNumInQ == 0) {37                 ParentNumInQ = ChildNumInQ;38                 ChildNumInQ = 0;39                 pre.next = null;40                 pre = pre.next;41             }42         }43     }44 }

 

Leetcode: populating next right pointers in each node

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.