Last updated on October 5th, 2024 at 05:47 pm
Here, We see Populating Next Right Pointers in Each Node II LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.
List of all LeetCode Solution
Topics
Depth-First Search, Tree
Companies
Bloomberg, Facebook, Microsoft
Level of Question
Tree
Populating Next Right Pointers in Each Node II LeetCode Solution
Table of Contents
Problem Statement
Given a binary treestruct Node { int val; Node *left; Node *right; Node *next; }
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL
.
Initially, all next pointers are set to NULL
.
Example 1:
Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.
Example 2:
Input: root = []
Output: []
1. Populating Next Right Pointers in Each Node II LeetCode Solution C++
class Solution { public: Node* connect(Node* root) { Node *currParent = root, *baseChild, *currChild, *nextChild; while (currParent) { while (currParent->next && !currParent->left && !currParent->right) currParent = currParent->next; currChild = baseChild = currParent->left ? currParent->left : currParent->right; while (currChild) { if (currParent->right && currChild != currParent->right) nextChild = currParent->right; else { currParent = currParent->next; while (currParent && !currParent->left && !currParent->right) currParent = currParent->next; nextChild = currParent ? currParent->left ? currParent->left : currParent->right : currParent; } currChild->next = nextChild; currChild = nextChild; } currParent = baseChild; } return root; } };
2. Populating Next Right Pointers in Each Node II LeetCode Solution Java
class Solution { public Node connect(Node root) { if (root == null) { return root; } Queue<Node> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int levelCount = queue.size(); Node prev = null; for (int i = 0; i < levelCount; i++) { Node curNode = queue.poll(); if (prev != null) { prev.next = curNode; } prev = curNode; if (curNode.left != null) { queue.add(curNode.left); } if (curNode.right != null) { queue.add(curNode.right); } } } return root; } }
3. Populating Next Right Pointers in Each Node II Solution JavaScript
var connect = function(root) { if (!root) return root; let queue = [root]; let tempQueue = []; while(queue.length){ let curr = queue.splice(0, 1)[0]; let {left, right} = curr; if (left) tempQueue.push(left); if (right) tempQueue.push(right); if (queue.length === 0){ curr.next = null; queue = tempQueue; tempQueue = []; }else{ curr.next = queue[0]; } } return root; };
4. Populating Next Right Pointers in Each Node II Solution Python
class Solution(object): def connect(self, root): if not root: return None q = deque() q.append(root) dummy=Node(-999) while q: length=len(q) prev=dummy for _ in range(length): popped=q.popleft() if popped.left: q.append(popped.left) prev.next=popped.left prev=prev.next if popped.right: q.append(popped.right) prev.next=popped.right prev=prev.next return root