From 28add7d729035584621bc3db5ed6bb179cd5c4df Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Thu, 29 Jul 2021 11:58:29 +0800 Subject: [PATCH] add: leetcode 1104 solution --- ...104.Path In Zigzag Labelled Binary Tree.go | 43 +++++++++++++++++ ...ath In Zigzag Labelled Binary Tree_test.go | 45 ++++++++++++++++++ .../README.md | 47 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree.go create mode 100644 leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree_test.go create mode 100644 leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/README.md diff --git a/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree.go b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree.go new file mode 100644 index 00000000..a7e2f580 --- /dev/null +++ b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree.go @@ -0,0 +1,43 @@ +package leetcode + +func pathInZigZagTree(label int) []int { + level := getLevel(label) + ans := []int{label} + curIndex := label - (1 << level) + parent := 0 + for level >= 1 { + parent, curIndex = getParent(curIndex, level) + ans = append(ans, parent) + level-- + } + ans = reverse(ans) + return ans +} + +func getLevel(label int) int { + level := 0 + nums := 0 + for { + nums += 1 << level + if nums >= label { + return level + } + level++ + } +} + +func getParent(index int, level int) (parent int, parentIndex int) { + parentIndex = 1<<(level-1) - 1 + (index/2)*(-1) + parent = 1<<(level-1) + parentIndex + return +} + +func reverse(nums []int) []int { + left, right := 0, len(nums)-1 + for left < right { + nums[left], nums[right] = nums[right], nums[left] + left++ + right-- + } + return nums +} diff --git a/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree_test.go b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree_test.go new file mode 100644 index 00000000..5bdd0cf0 --- /dev/null +++ b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/1104.Path In Zigzag Labelled Binary Tree_test.go @@ -0,0 +1,45 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1104 struct { + para1104 + ans1104 +} + +// para 是参数 +type para1104 struct { + label int +} + +// ans 是答案 +type ans1104 struct { + ans []int +} + +func Test_Problem1104(t *testing.T) { + + qs := []question1104{ + + { + para1104{14}, + ans1104{[]int{1, 3, 4, 14}}, + }, + + { + para1104{26}, + ans1104{[]int{1, 2, 6, 10, 26}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1104------------------------\n") + + for _, q := range qs { + _, p := q.ans1104, q.para1104 + fmt.Printf("【input】:%v 【output】:%v \n", p, pathInZigZagTree(p.label)) + } + fmt.Printf("\n\n\n") +} diff --git a/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/README.md b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/README.md new file mode 100644 index 00000000..e97add3b --- /dev/null +++ b/leetcode/1104.Path-In-Zigzag-Labelled-Binary-Tree/README.md @@ -0,0 +1,47 @@ +# [1104. Path In Zigzag Labelled Binary Tree](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) + + +## 题目 + +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 first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left. + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/06/28/tree.png) + +Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label. + + +**Example 1:** + + Input: label = 14 + Output: [1,3,4,14] + +**Example 2:** + + Input: label = 26 + Output: [1,2,6,10,26] + +**Constraints:** + +1. `1 <= label <= 10^6` + +## 题目大意 + +在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 逐行 依次按 “之” 字形进行标记。 + +如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标记; + +而偶数行(即,第二行、第四行、第六行……)中,按从右到左的顺序进行标记。 + +![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/06/28/tree.png) + +给你树上某一个节点的标号 label,请你返回从根节点到该标号为 label 节点的路径,该路径是由途经的节点标号所组成的。 + + + +## 解题思路 + +- 计算出label所在的level和index +- 根据index和level计算出父节点的index和value +- level减一,循环计算出对应的父节点直到根节点 \ No newline at end of file