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