{"id":5276,"date":"2019-07-01T21:40:47","date_gmt":"2019-07-02T04:40:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5276"},"modified":"2019-07-01T21:41:09","modified_gmt":"2019-07-02T04:41:09","slug":"leetcode-1104-path-in-zigzag-labelled-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1104-path-in-zigzag-labelled-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1104. Path In Zigzag Labelled Binary Tree"},"content":{"rendered":"\n<p>In an infinite binary tree where every node has two children, the nodes are labelled in row order.<\/p>\n\n\n\n<p>In the odd numbered rows (ie., the first, third, fifth,&#8230;), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,&#8230;), the labelling is right to left.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/06\/24\/tree.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Given the&nbsp;<code>label<\/code>&nbsp;of a node in this tree, return the labels in the path from the root of the tree to the&nbsp;node with that&nbsp;<code>label<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> label = 14\n<strong>Output:<\/strong> [1,3,4,14]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> label = 26\n<strong>Output:<\/strong> [1,2,6,10,26]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= label &lt;= 10^6<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> pathInZigZagTree(int label) {\n    deque<int> ans;\n    while (label) {\n      ans.push_front(label);\n      int h = 31 - __builtin_clz(label);\n      label = ((1 << h) + (1 << (h + 1)) - 1 - label) \/ 2;\n    }    \n    return {begin(ans), end(ans)};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In an infinite binary tree where every node has two children, the nodes are labelled in row order. In the odd numbered rows (ie., the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[222,31],"class_list":["post-5276","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"predecessor-version":[{"id":5278,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5276\/revisions\/5278"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}