Leetcode:populating Next Right pointers in each Node

Source: Internet
Author: User

Topic:
Given a binary tree

struct TreeLinkNode {  TreeLinkNode *left;  TreeLinkNode *right;  TreeLinkNode *next;}

Populate each of the next pointer to the next right node. If There is no next right node, the next pointer should are set to NULL.

Initially, all next pointers is 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  7

After calling your function, the tree is should look like:

     1 -> NULL   /    2 -> 3 -> NULL / \  / 4->5->6->7 -> NULL

Thinking Analysis:
We first traverse the binary tree using the queueing queue hierarchy to put the result into the vector because the test instructions hypothesis is a fully binary tree, so each layer of the binary tree is a 2i node. Using this rule we can get the nodes of each layer, and then loop the first node from each layer to the right after one node.

C + + Reference code:

/** * Definition for binary tree with next pointer. * struct TREELINKNODE {* int val; * Treelinknode *left, *right, *next; * Treelinknode (int x): Val (x), left (null), Right (null), Next (null) {}}; */classsolution{ Public:voidConnect (Treelinknode *root) {if(!root)return; queue<TreeLinkNode*>Nodequeue; vector<TreeLinkNode*>Nodevector;//Two fork tree hierarchy traversal resultsNodequeue.push (root); Treelinknode *treenode =nullptr;//Two fork tree hierarchy traversal         while(!nodequeue.empty ())            {TreeNode = Nodequeue.front ();            Nodequeue.pop (); Nodevector.push_back (TreeNode);if(treenode->left) Nodequeue.push (treenode->left);if(treenode->right) Nodequeue.push (treenode->right); } vector<TreeLinkNode*>:: Size_type size = Nodevector.size ();intCount =0;//The node pointer during traversal        intLevel =0;//layer of the tree        intStep =0;//record number of tree nodes over two         while(Count < size) {Step + =int(POW(2, level)); while(Count < Step-1) && (count < size) {Nodevector[count]->next = Nodevector[++count];            } count++;        level++; }    }};

Java Reference Code:

/** * Definition for binary tree with next pointer. * public class Treelinknode {* int val; * Treelinknode le FT, right, next; * Treelinknode (int x) {val = x;}} * * Public  class solution {     Public void Connect(Treelinknode Root) {if(Root = =NULL)return; Queue<treelinknode> Nodequeue =NewLinkedlist<treelinknode> (); List<treelinknode> nodeList =NewArraylist<treelinknode> (); Treelinknode TreeNode =NULL; Nodequeue.offer (root);//level traverse binary tree, result into nodelist         while(!nodequeue.isempty ())            {TreeNode = Nodequeue.poll (); Nodelist.add (TreeNode);if(Treenode.left! =NULL) Nodequeue.offer (Treenode.left);if(Treenode.right! =NULL) Nodequeue.offer (treenode.right); }intSize = Nodelist.size ();intCount =0;intLevel =0;intStep =0; while(Count < size) {Step + = Math.pow (2, level); while(Count < Step-1&& (count < size)) {Nodelist.get (count). Next = Nodelist.get (++count);            } ++count;        ++level; }    }}

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.