mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 144
This commit is contained in:
@ -0,0 +1,39 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func preorderTraversal(root *TreeNode) []int {
|
||||
res := []int{}
|
||||
if root != nil {
|
||||
res = append(res, root.Val)
|
||||
tmp := preorderTraversal(root.Left)
|
||||
for _, t := range tmp {
|
||||
res = append(res, t)
|
||||
}
|
||||
tmp = preorderTraversal(root.Right)
|
||||
for _, t := range tmp {
|
||||
res = append(res, t)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func preorderTraversal_(root *TreeNode) []int {
|
||||
var result []int
|
||||
preorder(root, &result)
|
||||
return result
|
||||
}
|
||||
|
||||
func preorder(root *TreeNode, output *[]int) {
|
||||
if root != nil {
|
||||
*output = append(*output, root.Val)
|
||||
preorder(root.Left, output)
|
||||
preorder(root.Right, output)
|
||||
}
|
||||
}
|
@ -0,0 +1,213 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question144 struct {
|
||||
para144
|
||||
ans144
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para144 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans144 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem144(t *testing.T) {
|
||||
|
||||
qs := []question144{
|
||||
|
||||
question144{
|
||||
para144{[]int{}},
|
||||
ans144{[]int{}},
|
||||
},
|
||||
|
||||
question144{
|
||||
para144{[]int{1}},
|
||||
ans144{[]int{1}},
|
||||
},
|
||||
|
||||
question144{
|
||||
para144{[]int{1, NULL, 2, 3}},
|
||||
ans144{[]int{1, 2, 3}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 144------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans144, q.para144
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
root := Ints2TreeNode(p.one)
|
||||
fmt.Printf("【output】:%v \n", preorderTraversal(root))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
|
||||
var NULL = -1 << 63
|
||||
|
||||
// Ints2TreeNode 利用 []int 生成 *TreeNode
|
||||
func Ints2TreeNode(ints []int) *TreeNode {
|
||||
n := len(ints)
|
||||
if n == 0 {
|
||||
return nil
|
||||
}
|
||||
root := &TreeNode{
|
||||
Val: ints[0],
|
||||
}
|
||||
queue := make([]*TreeNode, 1, n*2)
|
||||
queue[0] = root
|
||||
i := 1
|
||||
for i < n {
|
||||
node := queue[0]
|
||||
queue = queue[1:]
|
||||
if i < n && ints[i] != NULL {
|
||||
node.Left = &TreeNode{Val: ints[i]}
|
||||
queue = append(queue, node.Left)
|
||||
}
|
||||
i++
|
||||
if i < n && ints[i] != NULL {
|
||||
node.Right = &TreeNode{Val: ints[i]}
|
||||
queue = append(queue, node.Right)
|
||||
}
|
||||
i++
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func indexOf(val int, nums []int) int {
|
||||
for i, v := range nums {
|
||||
if v == val {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("%d 不存在于 %v 中", val, nums)
|
||||
panic(msg)
|
||||
}
|
||||
|
||||
// PreIn2Tree 把 preorder 和 inorder 切片转换成 二叉树
|
||||
func PreIn2Tree(pre, in []int) *TreeNode {
|
||||
if len(pre) != len(in) {
|
||||
panic("preIn2Tree 中两个切片的长度不相等")
|
||||
}
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
res := &TreeNode{
|
||||
Val: pre[0],
|
||||
}
|
||||
if len(in) == 1 {
|
||||
return res
|
||||
}
|
||||
idx := indexOf(res.Val, in)
|
||||
res.Left = PreIn2Tree(pre[1:idx+1], in[:idx])
|
||||
res.Right = PreIn2Tree(pre[idx+1:], in[idx+1:])
|
||||
return res
|
||||
}
|
||||
|
||||
// InPost2Tree 把 inorder 和 postorder 切片转换成 二叉树
|
||||
func InPost2Tree(in, post []int) *TreeNode {
|
||||
if len(post) != len(in) {
|
||||
panic("inPost2Tree 中两个切片的长度不相等")
|
||||
}
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
res := &TreeNode{
|
||||
Val: post[len(post)-1],
|
||||
}
|
||||
if len(in) == 1 {
|
||||
return res
|
||||
}
|
||||
idx := indexOf(res.Val, in)
|
||||
res.Left = InPost2Tree(in[:idx], post[:idx])
|
||||
res.Right = InPost2Tree(in[idx+1:], post[idx:len(post)-1])
|
||||
return res
|
||||
}
|
||||
|
||||
// Tree2Preorder 把 二叉树 转换成 preorder 的切片
|
||||
func Tree2Preorder(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
if root.Left == nil && root.Right == nil {
|
||||
return []int{root.Val}
|
||||
}
|
||||
res := []int{root.Val}
|
||||
res = append(res, Tree2Preorder(root.Left)...)
|
||||
res = append(res, Tree2Preorder(root.Right)...)
|
||||
return res
|
||||
}
|
||||
|
||||
// Tree2Inorder 把 二叉树转换成 inorder 的切片
|
||||
func Tree2Inorder(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
if root.Left == nil && root.Right == nil {
|
||||
return []int{root.Val}
|
||||
}
|
||||
res := Tree2Inorder(root.Left)
|
||||
res = append(res, root.Val)
|
||||
res = append(res, Tree2Inorder(root.Right)...)
|
||||
return res
|
||||
}
|
||||
|
||||
// Tree2Postorder 把 二叉树 转换成 postorder 的切片
|
||||
func Tree2Postorder(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
if root.Left == nil && root.Right == nil {
|
||||
return []int{root.Val}
|
||||
}
|
||||
res := Tree2Postorder(root.Left)
|
||||
res = append(res, Tree2Postorder(root.Right)...)
|
||||
res = append(res, root.Val)
|
||||
return res
|
||||
}
|
||||
|
||||
// Equal return ture if tn == a
|
||||
func (tn *TreeNode) Equal(a *TreeNode) bool {
|
||||
if tn == nil && a == nil {
|
||||
return true
|
||||
}
|
||||
if tn == nil || a == nil || tn.Val != a.Val {
|
||||
return false
|
||||
}
|
||||
return tn.Left.Equal(a.Left) && tn.Right.Equal(a.Right)
|
||||
}
|
||||
|
||||
// Tree2ints 把 *TreeNode 按照行还原成 []int
|
||||
func Tree2ints(tn *TreeNode) []int {
|
||||
res := make([]int, 0, 1024)
|
||||
queue := []*TreeNode{tn}
|
||||
for len(queue) > 0 {
|
||||
size := len(queue)
|
||||
for i := 0; i < size; i++ {
|
||||
nd := queue[i]
|
||||
if nd == nil {
|
||||
res = append(res, NULL)
|
||||
} else {
|
||||
res = append(res, nd.Val)
|
||||
queue = append(queue, nd.Left, nd.Right)
|
||||
}
|
||||
}
|
||||
queue = queue[size:]
|
||||
}
|
||||
i := len(res)
|
||||
for i > 0 && res[i-1] == NULL {
|
||||
i--
|
||||
}
|
||||
return res[:i]
|
||||
}
|
33
Algorithms/144. Binary Tree Preorder Traversal/README.md
Normal file
33
Algorithms/144. Binary Tree Preorder Traversal/README.md
Normal file
@ -0,0 +1,33 @@
|
||||
# [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary tree, return the preorder traversal of its nodes' values.
|
||||
|
||||
|
||||
|
||||
Example :
|
||||
|
||||
```c
|
||||
Input: [1,null,2,3]
|
||||
1
|
||||
\
|
||||
2
|
||||
/
|
||||
3
|
||||
|
||||
Output: [1,2,3]
|
||||
```
|
||||
|
||||
|
||||
Follow up: Recursive solution is trivial, could you do it iteratively?
|
||||
|
||||
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
先根遍历一颗树。两种递归的实现方法,见代码。
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user