diff --git a/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View.go b/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View.go new file mode 100644 index 00000000..70454692 --- /dev/null +++ b/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View.go @@ -0,0 +1,41 @@ +package leetcode + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func rightSideView(root *TreeNode) []int { + if root == nil { + return []int{} + } + queue := []*TreeNode{} + queue = append(queue, root) + curNum, nextLevelNum, res, tmp := 1, 0, []int{}, []int{} + for len(queue) != 0 { + if curNum > 0 { + node := queue[0] + if node.Left != nil { + queue = append(queue, node.Left) + nextLevelNum++ + } + if node.Right != nil { + queue = append(queue, node.Right) + nextLevelNum++ + } + curNum-- + tmp = append(tmp, node.Val) + queue = queue[1:] + } + if curNum == 0 { + res = append(res, tmp[len(tmp)-1]) + curNum = nextLevelNum + nextLevelNum = 0 + tmp = []int{} + } + } + return res +} diff --git a/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View_test.go b/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View_test.go new file mode 100644 index 00000000..dcb8b2cb --- /dev/null +++ b/Algorithms/199. Binary Tree Right Side View/199. Binary Tree Right Side View_test.go @@ -0,0 +1,59 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question199 struct { + para199 + ans199 +} + +// para 是参数 +// one 代表第一个参数 +type para199 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans199 struct { + one []int +} + +func Test_Problem199(t *testing.T) { + + qs := []question199{ + + question199{ + para199{[]int{}}, + ans199{[]int{}}, + }, + + question199{ + para199{[]int{1}}, + ans199{[]int{1}}, + }, + + question199{ + para199{[]int{3, 9, 20, NULL, NULL, 15, 7}}, + ans199{[]int{3, 20, 7}}, + }, + + question199{ + para199{[]int{1, 2, 3, 4, NULL, NULL, 5}}, + ans199{[]int{1, 3, 5}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 199------------------------\n") + + for _, q := range qs { + _, p := q.ans199, q.para199 + fmt.Printf("【input】:%v ", p) + root := Ints2TreeNode(p.one) + fmt.Printf("【output】:%v \n", rightSideView(root)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/199. Binary Tree Right Side View/README.md b/Algorithms/199. Binary Tree Right Side View/README.md new file mode 100644 index 00000000..3d65beb7 --- /dev/null +++ b/Algorithms/199. Binary Tree Right Side View/README.md @@ -0,0 +1,29 @@ +# [199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) + +## 题目 + +Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. + +Example: + +```c +Input: [1,2,3,null,5,null,4] +Output: [1, 3, 4] +Explanation: + + 1 <--- + / \ +2 3 <--- + \ \ + 5 4 <--- +``` + + + +## 题目大意 + +这一题是按层序遍历的变种题。按照层序把每层的元素都遍历出来,然后依次取每一层的最右边的元素即可。用一个队列即可实现。 + +第 102 题和第 107 题都是按层序遍历的。 + +