当前位置:网站首页>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
}
边栏推荐
- Leetcode-6110: number of incremental paths in the grid graph
- Typical use cases for knapsacks, queues, and stacks
- 2022年贵州省职业院校技能大赛中职组网络安全赛项规程
- Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
- Leetcode-6108: decrypt messages
- 【LeetCode】Day95-有效的数独&矩阵置零
- Overview of variable resistors - structure, operation and different applications
- Leetcode stack related
- Arduino 控制的 RGB LED 无限镜
- 1041 Be Unique
猜你喜欢

Open source storage is so popular, why do we insist on self-development?

Spark中groupByKey() 和 reduceByKey() 和combineByKey()

LVS简介【暂未完成(半成品)】

做 SQL 性能优化真是让人干瞪眼

可变电阻器概述——结构、工作和不同应用

LeetCode 0107.二叉树的层序遍历II - 另一种方法

Overview of variable resistors - structure, operation and different applications

Doing SQL performance optimization is really eye-catching

Some common problems in the assessment of network engineers: WLAN, BGP, switch

Redis publish subscribe command line implementation
随机推荐
[practical skills] how to do a good job in technical training?
leetcode-556:下一个更大元素 III
LeetCode 0107.二叉树的层序遍历II - 另一种方法
Smart construction site "hydropower energy consumption online monitoring system"
1041 Be Unique
MySQL advanced part 1: triggers
[rust notes] 14 set (Part 2)
【Rust 笔记】17-并发(上)
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
1039 Course List for Student
Daily question 1189 Maximum number of "balloons"
SPI 详解
Records of some tools 2022
Sword finger offer II 058: schedule
Leetcode-6109: number of people who know secrets
Leetcode-556: the next larger element III
Collection: programming related websites and books
Leetcode-3: Longest substring without repeated characters
可变电阻器概述——结构、工作和不同应用
开源存储这么香,为何我们还要坚持自研?