Comments (5)
Hello! : ) Sorry to have kept you waiting. I've solved this problem and push a commit.
structures/TreeNode.go
// Strings2TreeNode converts []string to *TreeNode
func Strings2TreeNode(strs []string) *TreeNode {
n := len(strs)
if n == 0 {
return nil
}
x, _ := strconv.Atoi(strs[0])
root := &TreeNode{Val: x}
queue := make([]*TreeNode, 1, n<<1)
queue[0] = root
i := 1
for i < n {
node := queue[0]
queue = queue[1:]
if i < n && strs[i] != "null" {
x, _ = strconv.Atoi(strs[i])
node.Left = &TreeNode{Val: x}
queue = append(queue, node.Left)
}
i++
if i < n && strs[i] != "null" {
x, _ = strconv.Atoi(strs[i])
node.Right = &TreeNode{Val: x}
queue = append(queue, node.Right)
}
i++
}
return root
}
// Tree2LevelOrderStrings converts *TreeNode into []string by level order traversal.
func Tree2LevelOrderStrings(root *TreeNode) []string {
var ans []string
if root == nil {
return ans
}
queue := []*TreeNode{root}
var level int
for level = 0; len(queue) > 0; level++ {
size := len(queue)
for i := 0; i < size; i++ {
node := queue[i]
if node == nil {
ans = append(ans, "null")
} else {
ans = append(ans, strconv.Itoa(node.Val))
if node.Left != nil || node.Right != nil {
queue = append(queue, node.Left, node.Right)
}
}
}
queue = queue[size:]
}
level--
return ans
}
- Flatten Binary Tree to Linked List_test.go
func Test_Problem114(t *testing.T) {
qs := []question114{
{
para114{[]string{"1", "2", "5", "3", "4", "null", "6"}},
ans114{[]string{"1", "null", "2", "null", "3", "null", "4", "null", "5", "null", "6"}},
},
{
para114{[]string{"0"}},
ans114{[]string{"0"}},
},
{
para114{[]string{"1", "2", "3", "4", "5", "6"}},
ans114{[]string{"1", "null", "2", "null", "3", "null", "4", "null", "5", "null", "6"}},
},
}
fmt.Printf("------------------------Leetcode Problem 114------------------------\n")
for _, q := range qs {
_, p := q.ans114, q.para114
fmt.Printf("【input】:%v \n", p)
rootOne := structures.Strings2TreeNode(p.one)
flatten(rootOne)
fmt.Printf("【output】:%v \n", structures.Tree2LevelOrderStrings(rootOne))
}
fmt.Printf("\n\n\n")
}
The following is the output of go test under Windows system:
D:\Code\LeetCode-Go\leetcode\0114.Flatten-Binary-Tree-to-Linked-List>go test
------------------------Leetcode Problem 114------------------------
【input】:{[1 2 5 3 4 null 6]}
【output】:[1 null 2 null 3 null 4 null 5 null 6]
【input】:{[0]}
【output】:[0]
【input】:{[1 2 3 4 5 6]}
【output】:[1 null 2 null 4 null 5 null 3 null 6]
PASS
ok github.com/halfrost/LeetCode-Go/leetcode/0114.Flatten-Binary-Tree-to-Linked-List 0.139s
from leetcode-go.
I'm stupid, because I outputed the levelorder traversal rather than preorder traversal in test.go. But look this draft when you input [1, 2, 3, 4, 5, 6], the expected output should be [1, 2, 4, 5, 3, 6]. Read the problem description carefully please!
from leetcode-go.
Reason:
Ints2TreeNode is not a preorder deserialization
Possible Solution:
0. rootOne := structures.Ints2TreeNode(p.one)
1. inputSlice := structures.Tree2Preorder(rootOne)
2. flatten(rootOne)
3. result := structures.Tree2Preorder(rootOne)
then compare inputSlice with result
from leetcode-go.
Assertion may help avoid this kind of negligence?
from leetcode-go.
@Don2025 Thanks
from leetcode-go.
Related Issues (20)
- A problem in 707. Design Linked List HOT 1
- Clone Error HOT 1
- 918. Maximum Sum Circular Subarray HOT 4
- 763 PartitionLabels 时间复杂度 HOT 1
- 关于此电子书如何搭建 HOT 5
- Windows系统下 关于 Clone 的问题 HOT 7
- When click the 's' key in the keyboard will trigger website search input box to be enable
- 是否可以提供epub版本 HOT 2
- Query: How book is generated? HOT 1
- Query: What is the technology behind web ? thank you HOT 5
- 怎么开启暗黑模式 HOT 3
- 0004 bug
- Can you re-arrange the layout of PDF to put the title and solution of question on the same page? HOT 1
- Hacking Facebook account HOT 4
- LeetCode第七题Reverse-Integer HOT 2
- leetcode 2038题, segment Tree的模板 HOT 1
- 1004 Max Consecutive Ones III HOT 3
- 霜神,你好,我有时候并最优解的go代码转成python之后,时空复杂度就变的很差,是不是语言的问题?目前上传的这一千多道题都基本上是go的最优解吗? HOT 4
- 网站证书过期,无法访问 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode-go.