Similar Problems
Similar Problems not available
Path In Zigzag Labelled Binary Tree - Leetcode Solution
Companies:
LeetCode: Path In Zigzag Labelled Binary Tree Leetcode Solution
Difficulty: Medium
Topics: math tree binary-tree
Problem Statement:
In an infinite binary tree where every node has two children, the nodes are labelled in row order. In other words, the label of the root is 1, the label of its two children is 2 and 3, the label of the fourth node is 4, the label of its two children is 5,6 and so on.
Given the label of a node in this tree, return the labels in the path from the root of the tree to the node.
Example:
Input: label = 14 Output: [1,3,4,14]
Solution:
The given binary tree is labelled in a special way, with each node labelled in row order. We can use this labeling system to our advantage to determine the path from the root node to any other node in the tree.
To find the path from the root node to the given node, we can follow the following steps:
- Determine the level of the given node.
- Reverse the labeling of the nodes at that level.
- Determine the path from the root node to the node by using the reversed labelling.
Let's look at each of these steps in more detail.
Step 1: Determine the level of the given node.
The level of a node in a binary tree is determined by finding the number of times we can divide the node's label by 2 before we reach 1. For example, the level of node 14 is 4, because 14/2 = 7, 7/2 = 3, 3/2 = 1.
We can find the level of the given node by repeatedly dividing the node's label by 2 until we reach 1. We will also keep track of the nodes we encounter in this process, so that we can use them later to reverse the labelling.
Here is the code for this step:
int level = 0;
vector<int> nodes;
while (label > 0) {
nodes.push_back(label);
label /= 2;
level++;
}
Step 2: Reverse the labeling of the nodes at that level.
Once we have determined the level of the given node, we can use it to reverse the labeling of the nodes at that level. To do this, we first determine the maximum label at that level (which is equal to 2^level - 1), and subtract the label of each node at that level from the maximum label. This will give us the reversed label of each node at that level.
Here is the code for this step:
int max_label = pow(2, level) - 1;
for (int i = 0; i < nodes.size(); i++) {
nodes[i] = max_label - nodes[i];
}
Step 3: Determine the path from the root node to the node by using the reversed labelling.
Now that we have the reversed label of each node at the level of the given node, we can determine the path from the root node to the node by following the reversed labelling and dividing each node's label by 2.
Here is the code for this step:
vector<int> path;
for (int i = nodes.size() - 1; i >= 0; i--) {
path.push_back(nodes[i]);
label = nodes[i] / 2;
}
path.push_back(1);
reverse(path.begin(), path.end());
return path;
We first initialize an empty vector to store the path. We then loop through the reversed labelling and compute the label of each node in the path by dividing by 2. We also add each node's label to the path vector.
Finally, we add the label of the root node (which is always 1) to the path vector and reverse it, so that the path is in the correct order.
Full Solution:
class Solution {
public:
vector<int> pathInZigZagTree(int label) {
int level = 0;
vector<int> nodes;
while (label > 0) {
nodes.push_back(label);
label /= 2;
level++;
}
int max_label = pow(2, level) - 1;
for (int i = 0; i < nodes.size(); i++) {
nodes[i] = max_label - nodes[i];
}
vector<int> path;
for (int i = nodes.size() - 1; i >= 0; i--) {
path.push_back(nodes[i]);
label = nodes[i] / 2;
}
path.push_back(1);
reverse(path.begin(), path.end());
return path;
}
};
Time Complexity: O(log N)
Space Complexity: O(log N), where N is the label of the given node.
Path In Zigzag Labelled Binary Tree Solution Code
1