← Home

1104. Path In Zigzag Labelled Binary Tree

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
110

Let's make this one less mysterious. For 1104. Path In Zigzag Labelled Binary Tree, the solution in this repository is mainly a two pointers solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers, sliding window.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are pathInZigZagTree.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Path In Zigzag Labelled Binary Tree.
02//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Path In Zigzag Labelled Binary Tree.
03
04class Solution {
05public:
06    vector<int> pathInZigZagTree(int label) {
07        int level = int(log2(label)); //index starts from 0
08        vector<int> path;
09        int node;
10        int index; //the index in a level, starts from 0, from left to right
11        path.push_back(label);
12        
13        node = label;
14        while(node != 1){
15            if(level % 2 == 0){
16                //label from left to right
17                index = node - pow(2, level);
18                //get the index of its parent
19                index /= 2;
20                //pow(2, level) - 1: the 0th element of last level
21                // - index: because in the last level, label is from right to left
22                node = (pow(2, level) - 1) - index;
23            }else{
24                //label from right to left
25                //(pow(2, level)-1) - xxx: to change the direction of the index in a level
26                index = (pow(2, level)-1) - (node - pow(2, level));
27                //get the index of its parent
28                index /= 2;
29                node = pow(2, level-1) + index;
30            }
31            path.insert(path.begin(), node);
32            level--;
33            // cout << node << " ";
34        };
35        
36        return path;
37    }
38};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.