当前位置:网站首页>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
}
边栏推荐
猜你喜欢
[practical skills] technical management of managers with non-technical background
1.15 - input and output system
Typical use cases for knapsacks, queues, and stacks
MIT-6874-Deep Learning in the Life Sciences Week 7
6. Logistic model
Leetcode-6111: spiral matrix IV
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
SPI details
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
SQLMAP使用教程(二)实战技巧一
随机推荐
SQLMAP使用教程(二)实战技巧一
Erreur de connexion Navicat à la base de données Oracle Ora - 28547 ou Ora - 03135
How to adjust bugs in general projects ----- take you through the whole process by hand
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
1.15 - input and output system
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
传统数据库逐渐“难适应”,云原生数据库脱颖而出
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
LeetCode 0107. Sequence traversal of binary tree II - another method
Open source storage is so popular, why do we insist on self-development?
Leetcode-9: palindromes
Introduction et expérience de wazuh open source host Security Solution
Transform optimization problems into decision-making problems
Daily question 1688 Number of matches in the competition
Records of some tools 2022
SPI details
TypeScript 基础讲解
Daily question 1984 Minimum difference in student scores
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Sqlmap tutorial (1)