Populating Next Right Pointers in Each Node @ LeetCode

Source: Internet
Author: User

Package Level3; import Utility. treeLinkNode;/*** Populating Next Right Pointers in Each Node *** 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 shoshould be set to NULL. initially, all next pointers are set to NULL. note: You may only use co Nstant 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 7 After calling your function, the tree shoshould look like: 1-> NULL/\ 2-> 3-> NULL/\ 4-> 5-> 6-> 7-> NULL **/public class S116 {public static void main (String [] args) {} publi C static void connect (TreeLinkNode root) {// if (root = null) {return ;}// the left node is not empty, connect to the right node if (root. left! = Null) {root. left. next = root. right;} // you can use root. next to process the scenario of 6-to-6 connections. if (root. right! = Null & root. next! = Null) {root. right. next = root. next. left;} connect (root. left); connect (root. right );}}

 

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.