当前位置:网站首页>Traversal of leetcode tree
Traversal of leetcode tree
2022-07-05 06:14:00 【Dawnlighttt】
List of articles
q94 Middle order traversal of binary trees
Answer key
func inorderTraversal(root *TreeNode) []int {
var res []int
if root == nil {
return res
}
var dfs func(root *TreeNode, res *[]int)
dfs = func(root *TreeNode, res *[]int) {
if root.Left != nil {
dfs(root.Left, res)
}
*res = append(*res, root.Val)
if root.Right != nil {
dfs(root.Right, res)
}
}
dfs(root, &res)
return res
}
q144 Preorder traversal of two tree
Answer key
func preorderTraversal(root *TreeNode) []int {
var res []int
if root == nil {
return res
}
var dfs func(root *TreeNode, res *[]int)
dfs = func(root *TreeNode, res *[]int) {
*res = append(*res, root.Val)
if root.Left != nil {
dfs(root.Left, res)
}
if root.Right != nil {
dfs(root.Right, res)
}
}
dfs(root, &res)
return res
}
q145 Postorder traversal of binary trees
Answer key
func postorderTraversal(root *TreeNode) []int {
var res []int
if root == nil {
return res
}
var dfs func(root *TreeNode, res *[]int)
dfs = func(root *TreeNode, res *[]int) {
if root.Left != nil {
dfs(root.Left, res)
}
if root.Right != nil {
dfs(root.Right, res)
}
*res = append(*res, root.Val)
}
dfs(root, &res)
return res
}
q102 The level traversal of binary tree
Answer key
Using queue implementation :
func levelOrder(root *TreeNode) [][]int {
var res [][]int
if root == nil {
return res
}
// Create a q queue , Join the nodes on the first floor every time
// Join the root node for the first time
q := []*TreeNode{
root}
// Keep joining the node on the first floor
for i := 0; len(q) > 0; i++ {
// Open a new floor
res = append(res, []int{
})
// p The queue is used to queue the child nodes of all nodes in the current layer , That is, the next element of the team
var p []*TreeNode
// Traverse the current layer node
for j := 0; j < len(q); j++ {
// Add all nodes of the current layer to res Array
node := q[j]
res[i] = append(res[i], node.Val)
// Join the next node
if node.Left != nil {
p = append(p, node.Left)
}
if node.Right != nil {
p = append(p, node.Right)
}
}
// Traverse the next layer
q = p
}
return res
}
Recursive implementation :
func levelOrder(root *TreeNode) [][]int {
var res [][]int
var dfs func(node *TreeNode, depth int)
dfs = func(node *TreeNode, depth int) {
// Recursive export
if node == nil {
return
}
// Initialize a new layer of storage space
if len(res) == depth {
res = append(res, []int{
})
}
// Will be the first depth The node of the layer is added to res in
res[depth] = append(res[depth], node.Val)
if node.Left != nil {
dfs(node.Left, depth + 1)
}
if node.Right != nil {
dfs(node.Right, depth + 1)
}
}
dfs(root, 0)
return res
}
q110 Balanced binary trees
Answer key
Find the depth of the binary tree and judge whether the left and right nodes are balanced .
func isBalanced(root *TreeNode) bool {
if root == nil {
return true
}
return abs(dfs(root.Left) - dfs(root.Right)) <= 1 && isBalanced(root.Left) && isBalanced(root.Right)
}
func dfs(t *TreeNode) int {
if t == nil {
return 0
}
lChild := dfs(t.Left)
rChild := dfs(t.Right)
return 1 + max(lChild, rChild)
}
func max(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
边栏推荐
- SQLMAP使用教程(二)实战技巧一
- One question per day 1765 The highest point in the map
- 2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
- 开源存储这么香,为何我们还要坚持自研?
- 快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
- Appium自动化测试基础 — Appium测试环境搭建总结
- LVS简介【暂未完成(半成品)】
- Spark中groupByKey() 和 reduceByKey() 和combineByKey()
- Daily question 1189 Maximum number of "balloons"
- 【Rust 笔记】17-并发(下)
猜你喜欢
Is it impossible for lamda to wake up?
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
1.13 - RISC/CISC
MIT-6874-Deep Learning in the Life Sciences Week 7
LVS简介【暂未完成(半成品)】
Leetcode-6111: spiral matrix IV
Data visualization chart summary (II)
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
Appium foundation - use the first demo of appium
[practical skills] how to do a good job in technical training?
随机推荐
Individual game 12
【Rust 笔记】17-并发(上)
【Rust 笔记】13-迭代器(中)
Leetcode-22: bracket generation
【Rust 笔记】15-字符串与文本(下)
R language [import and export of dataset]
“磐云杯”中职网络安全技能大赛A模块新题
Arduino 控制的 RGB LED 无限镜
Smart construction site "hydropower energy consumption online monitoring system"
[rust notes] 13 iterator (Part 2)
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
Daily question 2006 Number of pairs whose absolute value of difference is k
1.14 - 流水线
Leetcode-6111: spiral matrix IV
New title of module a of "PanYun Cup" secondary vocational network security skills competition
leetcode-6108:解密消息
[rust notes] 17 concurrent (Part 2)
[rust notes] 16 input and output (Part 2)
Usage scenarios of golang context
1040 Longest Symmetric String