当前位置:网站首页>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】Day94-重塑矩阵
- 【Rust 笔记】13-迭代器(下)
- leetcode-22:括号生成
- leetcode-556:下一个更大元素 III
- JS quickly converts JSON data into URL parameters
- 1.14 - assembly line
- Appium foundation - use the first demo of appium
- The difference between CPU core and logical processor
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
猜你喜欢
![R language [import and export of dataset]](/img/5e/a15ab692a6f049f846024c98820fbb.png)
R language [import and export of dataset]

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

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

Implement a fixed capacity stack

Leetcode stack related

Appium基础 — 使用Appium的第一个Demo

Data visualization chart summary (II)

1.14 - 流水线

Arduino 控制的 RGB LED 无限镜

Open source storage is so popular, why do we insist on self-development?
随机推荐
[rust notes] 17 concurrent (Part 2)
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Leetcode-6109: number of people who know secrets
6. Logistic model
Leetcode-9: palindromes
Transform optimization problems into decision-making problems
SPI 详解
LeetCode 1200.最小绝对差
leetcode-6109:知道秘密的人数
Bit mask of bit operation
Sword finger offer II 058: schedule
Daily question 1189 Maximum number of "balloons"
数据可视化图表总结(二)
【Rust 笔记】16-输入与输出(上)
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Leetcode-6111: spiral matrix IV
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
Open source storage is so popular, why do we insist on self-development?
A reason that is easy to be ignored when the printer is offline
7. Processing the input of multidimensional features