---
# System prepended metadata

title: 1104. Path In Zigzag Labelled Binary Tree
tags: [Leetcode, Tree, Medium]

---

# 1104. Path In Zigzag Labelled Binary Tree
###### tags: `Leetcode` `Medium` `Tree`
Link: https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/
## 思路
先找到正常情况下这个node的parent有哪些
然后再按照zigzag改一下parent
要注意的是改的时候要**从下往上算行数** 偶数的话不用变 奇数的话就revert一下
## Code
```java=
class Solution {
    public List<Integer> pathInZigZagTree(int label) {
        List<Integer> ans = new ArrayList<>();
        while(label>0){
            ans.add(0,label);
            label = label/2;
        }
        for(int i=ans.size()-1; i>=0; i--){
            if((ans.size()-1-i)%2==1){
                int rowMin = (int)Math.pow(2,i);
                int rowMax = (int)Math.pow(2,i+1)-1;
                int labelAfterZigZag = rowMax-(ans.get(i)-rowMin);
                ans.remove(i);
                ans.add(i, labelAfterZigZag);
            }
        }
        return ans;
    }
}
```