mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
添加 problem 94
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
var result []int
|
||||
inorder(root, &result)
|
||||
return result
|
||||
}
|
||||
|
||||
func inorder(root *TreeNode, output *[]int) {
|
||||
if root != nil {
|
||||
inorder(root.Left, output)
|
||||
*output = append(*output, root.Val)
|
||||
inorder(root.Right, output)
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question94 struct {
|
||||
para94
|
||||
ans94
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para94 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans94 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem94(t *testing.T) {
|
||||
|
||||
qs := []question94{
|
||||
|
||||
question94{
|
||||
para94{[]int{}},
|
||||
ans94{[]int{}},
|
||||
},
|
||||
|
||||
question94{
|
||||
para94{[]int{1}},
|
||||
ans94{[]int{1}},
|
||||
},
|
||||
|
||||
question94{
|
||||
para94{[]int{1, NULL, 2, 3}},
|
||||
ans94{[]int{1, 2, 3}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 94------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans94, q.para94
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", inorderTraversal(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
40
Algorithms/0094. Binary Tree Inorder Traversal/README.md
Normal file
40
Algorithms/0094. Binary Tree Inorder Traversal/README.md
Normal file
@ -0,0 +1,40 @@
|
||||
# [94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
|
||||
|
||||
## 题目
|
||||
|
||||
|
||||
Given a binary tree, return the inorder traversal of its nodes' values.
|
||||
|
||||
|
||||
|
||||
Example :
|
||||
|
||||
```c
|
||||
Input: [1,null,2,3]
|
||||
1
|
||||
\
|
||||
2
|
||||
/
|
||||
3
|
||||
|
||||
Output: [1,3,2]
|
||||
```
|
||||
|
||||
|
||||
Follow up: Recursive solution is trivial, could you do it iteratively?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
中根遍历一颗树。
|
||||
|
||||
## 解题思路
|
||||
|
||||
递归的实现方法,见代码。
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user