mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
二叉树的层序遍历 一套打八个JavaScript 迭代 + 递归版本
This commit is contained in:
@ -1100,6 +1100,276 @@ var levelOrder = function (root) {
|
||||
};
|
||||
```
|
||||
|
||||
> 二叉树的层序遍历(Javascript语言完全版) (迭代 + 递归)
|
||||
|
||||
```js
|
||||
/**
|
||||
* 102. 二叉树的层序遍历
|
||||
* @param {TreeNode} root
|
||||
* @return {number[][]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
|
||||
var levelOrder = function(root) {
|
||||
const queue = [], res = [];
|
||||
root && queue.push(root);
|
||||
while(len = queue.length) {
|
||||
const val = [];
|
||||
while(len--) {
|
||||
const node = queue.shift();
|
||||
val.push(node.val);
|
||||
node.left && queue.push(node.left);
|
||||
node.right && queue.push(node.right);
|
||||
}
|
||||
res.push(val);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 递归
|
||||
var levelOrder = function(root) {
|
||||
const res = [];
|
||||
function defs (root, i) {
|
||||
if(!root) return;
|
||||
if(!res[i]) res[i] = [];
|
||||
res[i].push(root.val)
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return res;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 107. 二叉树的层序遍历 II
|
||||
* @param {TreeNode} root
|
||||
* @return {number[][]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
|
||||
var levelOrderBottom = function(root) {
|
||||
const queue = [], res = [];
|
||||
root && queue.push(root);
|
||||
while(len = queue.length) {
|
||||
const val = [];
|
||||
while(len--) {
|
||||
const node = queue.shift();
|
||||
val.push(node.val);
|
||||
node.left && queue.push(node.left);
|
||||
node.right && queue.push(node.right);
|
||||
}
|
||||
res.push(val);
|
||||
}
|
||||
return res.reverse()
|
||||
};
|
||||
|
||||
// 递归
|
||||
|
||||
var levelOrderBottom = function(root) {
|
||||
const res = [];
|
||||
function defs (root, i) {
|
||||
if(!root) return;
|
||||
if(!res[i]) res[i] = [];
|
||||
res[i].push(root.val);
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return res.reverse();
|
||||
};
|
||||
|
||||
/**
|
||||
* 199. 二叉树的右视图
|
||||
* @param {TreeNode} root
|
||||
* @return {number[]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
|
||||
var rightSideView = function(root) {
|
||||
const res = [], queue = [];
|
||||
root && queue.push(root);
|
||||
while(l = queue.length) {
|
||||
while (l--) {
|
||||
const {val, left, right} = queue.shift();
|
||||
!l && res.push(val);
|
||||
left && queue.push(left);
|
||||
right && queue.push(right);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 递归
|
||||
var rightSideView = function(root) {
|
||||
const res = [];
|
||||
function defs(root, i) {
|
||||
if(!root) return;
|
||||
res[i] = root.val;
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
* 637. 二叉树的层平均值
|
||||
* @param {TreeNode} root
|
||||
* @return {number[]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
var averageOfLevels = function(root) {
|
||||
const stack = [], res = [];
|
||||
root && stack.push(root);
|
||||
while(len = stack.length) {
|
||||
let sum = 0, l = len;
|
||||
while(l--) {
|
||||
const {val, left, right} = stack.shift();
|
||||
sum += val;
|
||||
left && stack.push(left);
|
||||
right && stack.push(right);
|
||||
}
|
||||
res.push(sum/len);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 递归
|
||||
var averageOfLevels = function(root) {
|
||||
const resCount = [], res = [];
|
||||
function defs(root, i) {
|
||||
if(!root) return;
|
||||
if(isNaN(res[i])) resCount[i] = res[i] = 0;
|
||||
res[i] += root.val;
|
||||
resCount[i]++;
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return res.map((val, i) => val / resCount[i]);
|
||||
};
|
||||
|
||||
/**
|
||||
* 515. 在每个树行中找最大值
|
||||
* @param {TreeNode} root
|
||||
* @return {number[]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
const MIN_G = Number.MIN_SAFE_INTEGER;
|
||||
var largestValues = function(root) {
|
||||
const stack = [], res = [];
|
||||
root && stack.push(root);
|
||||
while(len = stack.length) {
|
||||
let max = MIN_G;
|
||||
while(len--) {
|
||||
const {val, left, right} = stack.shift();
|
||||
max = max > val ? max : val;
|
||||
left && stack.push(left);
|
||||
right && stack.push(right);
|
||||
}
|
||||
res.push(max);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 递归
|
||||
var largestValues = function(root) {
|
||||
const res = [];
|
||||
function defs (root, i) {
|
||||
if(!root) return;
|
||||
if(isNaN(res[i])) res[i] = root.val;
|
||||
res[i] = res[i] > root.val ? res[i] : root.val;
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
* 429. N 叉树的层序遍历
|
||||
* @param {Node|null} root
|
||||
* @return {number[][]}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
var levelOrder = function(root) {
|
||||
const stack = [], res = [];
|
||||
root && stack.push(root);
|
||||
while(len = stack.length) {
|
||||
const vals = [];
|
||||
while(len--) {
|
||||
const {val, children} = stack.shift();
|
||||
vals.push(val);
|
||||
for(const e of children) {
|
||||
stack.push(e);
|
||||
}
|
||||
}
|
||||
res.push(vals);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
// 递归
|
||||
|
||||
var levelOrder = function(root) {
|
||||
const res = [];
|
||||
function defs (root, i) {
|
||||
if(!root) return;
|
||||
if(!res[i]) res[i] = [];
|
||||
res[i].push(root.val);
|
||||
for(const e of root.children) {
|
||||
defs(e, i + 1);
|
||||
}
|
||||
}
|
||||
defs(root, 0);
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
* 116. 填充每个节点的下一个右侧节点指针
|
||||
* 117. 填充每个节点的下一个右侧节点指针 II
|
||||
* @param {Node} root
|
||||
* @return {Node}
|
||||
*/
|
||||
|
||||
// 迭代
|
||||
var connect = function(root) {
|
||||
const stack = [];
|
||||
root && stack.push(root);
|
||||
while(len = stack.length) {
|
||||
while(len--) {
|
||||
const node1 = stack.shift(),
|
||||
node2 = len ? stack[0] : null;
|
||||
node1.next = node2;
|
||||
node1.left && stack.push(node1.left);
|
||||
node1.right && stack.push(node1.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
};
|
||||
|
||||
// 递归
|
||||
var connect = function(root) {
|
||||
const res = [];
|
||||
function defs (root, i) {
|
||||
if(!root) return;
|
||||
if(res[i]) res[i].next = root;
|
||||
res[i] = root;
|
||||
root.left && defs(root.left, i + 1);
|
||||
root.right && defs(root.right, i + 1);
|
||||
}
|
||||
defs(root, 0);
|
||||
return root;
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user