-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1104.cs
33 lines (26 loc) · 1014 Bytes
/
Solution1104.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System.Text;
namespace LeetCode.Solutions;
public class Solution1104
{
/// <summary>
/// 1104. Path In Zigzag Labelled Binary Tree - Medium
/// <a href="https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree">See the problem</a>
/// </summary>
public IList<int> PathInZigZagTree(int label)
{
var path = new List<int>();
int row = (int)Math.Floor(Math.Log2(label)) + 1; // Find the row of the node
while (label > 0)
{
path.Add(label);
// Compute the true parent in a zigzag structure
int maxVal = (1 << row) - 1; // Maximum value in the current row
int minVal = (1 << (row - 1)); // Minimum value in the current row
// Compute the true parent using reversed logic
label = (maxVal + minVal - label) / 2;
row--; // Move one level up
}
path.Reverse(); // Reverse to show path from root to node
return path;
}
}