Array into linked list
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
function arrayToTree(arr) {
function toNode(item) {//转换数组项至节点
if (item === null || item === undefined) { return null }
else { return new TreeNode(item) }
}
let queue = [];
const tree = toNode(arr.shift());
queue.push(tree);//入队列第一个元素
while (arr.length > 0) {
//当数组里还有项的时候就拿数组的项去填充队列
let current = queue.shift();
current.left = toNode(arr.shift());
current.right = toNode(arr.shift());
if (current.left) { queue.push(current.left) }
if (current.right) { queue.push(current.right) }
}
return tree;
}
// var arr = [3, 9, 20, null, null, 15, 7]
// var arr = [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1];
// var arr = [3, 1, 4, null, 2]
var arr = [5, 3, 6, 2, 4, null, null, 1]
var data = new arrayToTree(arr)
console.log('data', data);
dfs
function dfs(root) {
const res = []
const dfs = (root) => {
res.push(root.val)
if (root.left) dfs(root.left)
if (root.right) dfs(root.right)
}
dfs(root)
return res
}
Recursive end condition
- After the program is finished, return undefined
Has a return value, return
bfs
function bfs(root) { const res = [] const queue = [root] while (queue.length > 0) { const cur = queue.shift() res.push(cur.val) if (cur.left) queue.push(cur.left) if (cur.right) queue.push(cur.right) } return res; }
102 per floor
var levelOrder = function (root) { const res = [] const queue = [root] while (queue.length > 0) { const levelSize = queue.length const levelRes = [] for (let i = 0; i < levelSize; i++) { const cur = queue.shift() levelRes.push(cur.val) if (cur.left) queue.push(cur.left) if (cur.right) queue.push(cur.right) } res.push(levelRes) } return res }
104 Maximum depth
var maxDepth = function (root) { if (!root) return 0 const queue = [root] let depth = 1 while (queue.length) { // 当前层的节点个数 const levelSize = queue.length // 逐个让当前层的节点出列 for (let i = 0; i < levelSize; i++) { // 当前出列的节点 const cur = queue.shift() // 左右子节点入列 if (cur.left) queue.push(cur.left) if (cur.right) queue.push(cur.right) } // 当前层所有节点已经出列,如果队列不为空,说明有下一层节点,depth+1 if (queue.length) depth++ } return depth }
111 minimum depth
const minDepth = (root) => {
if (!root) return 0;
const queue = [root]; // 根节点入列
let depth = 1; // 当前层的深度
while (queue.length) { // 直到清空队列
const levelSize = queue.length; // 当前层的节点个数
for (let i = 0; i < levelSize; i++) { // 遍历 逐个出列
const cur = queue.shift(); // 出列
if (cur.left == null && cur.right == null) { // 如果没有孩子,直接返回所在层数
return depth;
}
if (cur.left) queue.push(cur.left); // 有孩子,让孩子入列
if (cur.right) queue.push(cur.right);
}
depth++; // 肯定有下一层,如果没有早就return了
}
};
257 All Paths
Ideas
- dfs traversal
- temp appends the val of each layer, and res appends temp when it is a child node
- After the child node is added, delete the node in temp (ie pop()), return to the parent node of the child node (ie return), and add another child node of the parent node (ie sibling node)
For non-child nodes, append val to temp, execute pop(), and reach the last line of the dfs method, which is equivalent to ending the dfs function, similar to the return of a child node
function fn(root) {
const res = []
const temp = []
const dfs = (root) => {
if (root.val && !root.left && !root.right) {
temp.push(root.val)
res.push([...temp])
temp.pop()
return;
// 如当前是子节点{val:4,left:null,right:null},temp=[124],
// return的话,就会结束最顶层栈dfs(),回到{val:2,left:4,right:5},执行完 if (root.left) dfs(root.left),就会去执行 if (root.right) dfs(root.right)
}
temp.push(root.val)
if (root.left) dfs(root.left)
if (root.right) dfs(root.right)
temp.pop()
// 执行完if (root.right) dfs(root.right),就会执行temp.pop() ,temp=[12],变成temp=[1]
}
dfs(root)
return res;
}
112 Is there such a path?
function hasPathSum(root, sum) {
const res = []
const temp = []
const sumRes = []
const dfs = (root) => {
if (!root) return false;
if ((root.val || root.val === 0) && !root.left && !root.right) {
temp.push(root.val)
res.push([...temp])
temp.pop()
return
}
temp.push(root.val)
if (root.left) dfs(root.left)
if (root.right) dfs(root.right)
temp.pop()
}
dfs(root)
console.log('res', res);
for (let c of res) {
const result = c.reduce((prev, cur) => {
return prev + cur;
})
sumRes.push(result)
}
console.log('sumRes', sumRes);
return sumRes.includes(sum)
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。