当前位置:网站首页>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
}
边栏推荐
- Smart construction site "hydropower energy consumption online monitoring system"
- LeetCode 1200.最小绝对差
- 927. 三等分 模拟
- How to adjust bugs in general projects ----- take you through the whole process by hand
- JS quickly converts JSON data into URL parameters
- Regulations for network security events of vocational group in 2022 Guizhou Vocational College skill competition
- 【Rust 笔记】13-迭代器(中)
- TypeScript 基础讲解
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- MySQL advanced part 1: triggers
猜你喜欢

Wazuh开源主机安全解决方案的简介与使用体验

快速使用Amazon MemoryDB并构建你专属的Redis内存数据库

leetcode-6110:网格图中递增路径的数目

Introduction and experience of wazuh open source host security solution

Appium automation test foundation - Summary of appium test environment construction

The connection and solution between the shortest Hamilton path and the traveling salesman problem

Error ora-28547 or ora-03135 when Navicat connects to Oracle Database

Dichotomy, discretization, etc

Real time clock (RTC)

Sqlmap tutorial (II) practical skills I
随机推荐
Daily question 1688 Number of matches in the competition
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
开源存储这么香,为何我们还要坚持自研?
927. Trisection simulation
Doing SQL performance optimization is really eye-catching
【LeetCode】Day94-重塑矩阵
leetcode-6108:解密消息
[practical skills] how to do a good job in technical training?
[rust notes] 15 string and text (Part 1)
The sum of the unique elements of the daily question
MIT-6874-Deep Learning in the Life Sciences Week 7
SQLMAP使用教程(二)实战技巧一
Introduction and experience of wazuh open source host security solution
[rust notes] 13 iterator (Part 2)
liunx启动redis
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
剑指 Offer II 058:日程表
Typical use cases for knapsacks, queues, and stacks
leetcode-31:下一个排列