function bfs(root) {
let res = []
let queue = []
queue.push(root)
while (queue.length) {
let size = queue.length
for (let i = 0; i < size; i++) {
let node = queue.shift()
res.push(node.value)
if (node.left) {
queue.push(node.left)
}
if (node.right) {
queue.push(node.right)
}
}
}
return res
}
let res = []
function dfs(root) {
if (root == null) {
return
}
res.push(root.val)
dfs(root.left)
dfs(root.right)
}
function traverse(root) {
if (root == null) return
// root.val ===> 前
traverse(root.left)
// root.val ===> 中
traverse(root.right)
// root.val ===> 后
}
function getNthNode(head, k) {
let p = head
for (let i = 0; i < k; i++) {
p = p.next
}
let q = head
while (p != null) {
q = q.next
p = p.next
}
return p
}
function getNodeMiddle(head) {
let slow = head
let fast = head
while (fast != null && fast.next != null) {
slow = slow.next
fast = fast.next.next
}
return slow
}
function hasCycle(head) {
let slow = head
let fast = head
while (fast != null && fast.next != null) {
slow = slow.next
fast = fast.next.next
if (slow == fast) {
return true
}
}
return false
}