LeetCode_Study_Plan/Algorithm
116. Populating Next Right Pointers in Each Node
개발하는루루
2022. 12. 26. 14:22
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/description/
Populating Next Right Pointers in Each Node - LeetCode
Populating Next Right Pointers in Each Node - You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: struct Node { int val; Node *left; Node *right; Node
leetcode.com
class Solution {
public Node connect(Node root) {
connect(root, null);
return root;
}
private void connect(Node root, Node right) {
if (root == null) return;
root.next = right;
connect(root.left, root.right);
connect(root.right, right == null? null : root.next.left);
}
}