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