Skip to content

Commit 03286b1

Browse files
committed
Add solution #1104
1 parent 3235361 commit 03286b1

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,135 LeetCode solutions in JavaScript
1+
# 1,136 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -878,6 +878,7 @@
878878
1095|[Find in Mountain Array](./solutions/1095-find-in-mountain-array.js)|Hard|
879879
1096|[Brace Expansion II](./solutions/1096-brace-expansion-ii.js)|Hard|
880880
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
881+
1104|[Path In Zigzag Labelled Binary Tree](./solutions/1104-path-in-zigzag-labelled-binary-tree.js)|Medium|
881882
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
882883
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
883884
1123|[Lowest Common Ancestor of Deepest Leaves](./solutions/1123-lowest-common-ancestor-of-deepest-leaves.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 1104. Path In Zigzag Labelled Binary Tree
3+
* https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/
4+
* Difficulty: Medium
5+
*
6+
* In an infinite binary tree where every node has two children, the nodes are labelled
7+
* in row order.
8+
*
9+
* In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right,
10+
* while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.
11+
*
12+
* Given the label of a node in this tree, return the labels in the path from the root of the
13+
* tree to the node with that label.
14+
*/
15+
16+
/**
17+
* @param {number} label
18+
* @return {number[]}
19+
*/
20+
var pathInZigZagTree = function(label) {
21+
const result = [];
22+
let current = label;
23+
24+
while (current >= 1) {
25+
result.unshift(current);
26+
const level = Math.floor(Math.log2(current));
27+
current = Math.floor(((2 ** (level + 1)) - 1 + (2 ** level) - current) / 2);
28+
}
29+
30+
return result;
31+
};

0 commit comments

Comments
 (0)