Given the binary tree, how would you join nodes at each level from left to right. Say there are 5 nodes in the third level, connect all of them from left to right.
I do not need anyone to write code for this, but just an efficient algorithm.
thank
Create a vector of linked lists. Make DFS, tracking your level, and for each node found, add it to the linked level list. This will work in O (n), which is optimal.
Is that what you want to do?
:1. BFS.2. , - node node, . node node, node node.
public void BreadthFirstSearch(Action<Node> currentNodeAction) { Queue<Node> q = new Queue<Node>(); q.Enqueue(root); while (q.Count != 0) { Node current = q.Dequeue(); if (currentNodeAction != null) currentNodeAction(current); if (current.left != null) q.Enqueue(current.left); if (current.right != null) q.Enqueue(current.right); } } private void Linker(Node node) { Link(node.left, node.right); if (node.next != null) Link(node.right ?? node.left, node.next.left ?? node.next.right); } private void Link(Node node1, Node node2) { if (node1 != null && node2 != null) node1.next = node2; } public void LinkSameLevel() { BreadthFirstSearch(Linker); }
. , , / .
, , "" ( , - ), . node node . ( ). , node node, . , .
, . , .
, , 5- . node . DFS . , node .
. ( node , ), 1, , , 16. , (16), , (1 + 2 + 4 + 8 = 15), - O (n), n - .
node . 15 , node .
, , , .
#include <queue> struct Node { Node *left; Node *right; Node *next; }; /** Link all nodes of the same level in a binary tree. */ void link_level_nodes(Node *pRoot) { queue<Node*> q; Node *prev; // Pointer to the revious node of the current level Node *node; int cnt; // Count of the nodes in the current level int cntnext; // Count of the nodes in the next level if(NULL == pRoot) return; q.push(pRoot); cnt = 1; cntnext = 0; prev = NULL; while (!q.empty()) { node = q.front(); q.pop(); /* Add the left and the right nodes of the current node to the queue and increment the counter of nodes at the next level. */ if (node->left){ q.push(node->left); cntnext++; } if (node->right){ q.push(node->right); cntnext++; } /* Link the previous node of the current level to this node */ if (prev) prev->next = node; /* Se the previous node to the current */ prev = node; cnt--; if (0 == cnt) { // if this is the last node of the current level cnt = cntnext; cntnext = 0; prev = NULL; } } }
, node. , 0.
public Node(int d) { head=this; data=d; left=null; right=null; level=0; }
, , , . , , Vector of Nodes.
. , , -
1) BFS. , . , node node . node, dequeued node, , , .O (n).
2) , . .O (n).
3) (2), , , , , , + 1 . O (n ^ 2).
private class Node { public readonly Node Left; public readonly Node Right; public Node Link { get; private set; } public void Run() { LinkNext = null; } private Node LinkNext { get { return Link == null ? null : (Link.Left ?? Link.Right ?? Link.LinkNext); } set { Link = value; if (Right != null) Right.LinkNext = LinkNext; if (Left != null) Left.LinkNext = Right ?? LinkNext; } } }
Source: https://habr.com/ru/post/1730923/More articles:python log viewer application - pythonSlight buffer change tracking mode - emacsJSON and backslash - jsonImplementation of the state machine - c ++Convert anonymous type to array or arraylist. It can be done - c #OSX: получить окна приложений + границы - scriptingHow to determine the length of the MP3 file header? - c #Dynamically assigned table variables? - variablesFeedback, for example, www.monyta.com - javascriptHow to include file text in Java code? - javaAll Articles