-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
Leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
--- | ||
title: 105. Construct Binary Tree from Preorder and Inorder Traversal | ||
subtitle: "https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/" | ||
date: 2024-02-20T16:56:00+08:00 | ||
lastmod: 2024-02-20T16:56:00+08:00 | ||
draft: false | ||
author: "Kimi.Tsai" | ||
authorLink: "https://kimi0230.github.io/" | ||
description: "0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal" | ||
license: "" | ||
images: [] | ||
|
||
tags: [LeetCode, Go, Medium, Construct Binary Tree from Preorder and Inorder Traversal] | ||
categories: [LeetCode] | ||
|
||
featuredImage: "" | ||
featuredImagePreview: "" | ||
|
||
hiddenFromHomePage: false | ||
hiddenFromSearch: false | ||
twemoji: false | ||
lightgallery: true | ||
ruby: true | ||
fraction: true | ||
fontawesome: true | ||
linkToMarkdown: false | ||
rssFullText: false | ||
|
||
toc: | ||
enable: true | ||
auto: true | ||
code: | ||
copy: true | ||
maxShownLines: 200 | ||
math: | ||
enable: false | ||
# ... | ||
mapbox: | ||
# ... | ||
share: | ||
enable: true | ||
# ... | ||
comment: | ||
enable: true | ||
# ... | ||
library: | ||
css: | ||
# someCSS = "some.css" | ||
# located in "assets/" | ||
# Or | ||
# someCSS = "https://cdn.example.com/some.css" | ||
js: | ||
# someJS = "some.js" | ||
# located in "assets/" | ||
# Or | ||
# someJS = "https://cdn.example.com/some.js" | ||
seo: | ||
images: [] | ||
# ... | ||
--- | ||
# [105. Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/) | ||
|
||
## 題目 | ||
|
||
## 題目大意 | ||
|
||
|
||
## 解題思路 | ||
|
||
## Big O | ||
時間複雜 : `` | ||
空間複雜 : `` | ||
|
||
## 來源 | ||
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ | ||
* https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ | ||
|
||
## 解答 | ||
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/main.go | ||
|
||
```go | ||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
// 時間複雜 O(), 空間複雜 O() | ||
func buildTree(preorder []int, inorder []int) *TreeNode { | ||
if len(preorder) == 0 { | ||
return nil | ||
} | ||
result := &TreeNode{preorder[0], nil, nil} | ||
|
||
i := 0 | ||
for ; i < len(inorder); i++ { | ||
if preorder[0] == inorder[i] { | ||
break | ||
} | ||
} | ||
result.Left = buildTree(preorder[1:i+1], inorder[:i]) | ||
result.Right = buildTree(preorder[i+1:], inorder[i+1:]) | ||
|
||
return result | ||
} | ||
``` | ||
|
||
## Benchmark | ||
|
||
```sh | ||
|
||
``` |
28 changes: 28 additions & 0 deletions
28
Leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package constructbinarytreefrompreorderandinordertraversal | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
// 時間複雜 O(), 空間複雜 O() | ||
func buildTree(preorder []int, inorder []int) *TreeNode { | ||
if len(preorder) == 0 { | ||
return nil | ||
} | ||
result := &TreeNode{preorder[0], nil, nil} | ||
|
||
i := 0 | ||
for ; i < len(inorder); i++ { | ||
if preorder[0] == inorder[i] { | ||
break | ||
} | ||
} | ||
result.Left = buildTree(preorder[1:i+1], inorder[:i]) | ||
result.Right = buildTree(preorder[i+1:], inorder[i+1:]) | ||
|
||
return result | ||
} |
34 changes: 34 additions & 0 deletions
34
Leetcode/0105.Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal/main_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package | ||
|
||
import "testing" | ||
|
||
var tests = []struct { | ||
arg1 string | ||
want int | ||
}{ | ||
{ | ||
"bbbab", | ||
4, | ||
}, | ||
} | ||
|
||
func TestLongestPalindromeSubseq(t *testing.T) { | ||
for _, tt := range tests { | ||
// if got := ReverseList(tt.arg1); !reflect.DeepEqual(got, tt.want) { | ||
if got := LongestPalindromeSubseq(tt.arg1); got != tt.want { | ||
t.Errorf("got = %v, want = %v", got, tt.want) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLongestPalindromeSubseq(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
LongestPalindromeSubseq(tests[0].arg1) | ||
} | ||
} | ||
|
||
/* | ||
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=. | ||
*/ |