Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Ahmed k
// levelSize static void connect(TreeNode *root) { if (!root) return; ...

Ahmed k

Sep 13, 2022

// levelSize static void connect(TreeNode *root) { if (!root) return;

queue nodes_queue; nodes_queue.push(root);

while(!nodes_queue.empty()) { int levelSize = nodes_queue.size();

while(levelSize--) { TreeNode* current = nodes_queue.front();

if(current->left) nodes_queue.push(current->left); if(current->right) nodes_queue.push(current->right);

if(!levelSize) { // 0 current->next = NULL; nodes_queue.pop(); break; }

nodes_queue.pop();

current->next = nodes_queue.front(); } } }

0

0

Comments
Comments

On this page