Skip to content

Commit 18a37ec

Browse files
committed
二叉树的层序遍历 一套打八个JavaScript 迭代 + 递归版本
1 parent e9c91e7 commit 18a37ec

File tree

1 file changed

+270
-0
lines changed

1 file changed

+270
-0
lines changed

problems/0102.二叉树的层序遍历.md

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,276 @@ var levelOrder = function (root) {
11001100
};
11011101
```
11021102

1103+
> 二叉树的层序遍历(Javascript语言完全版) (迭代 + 递归)
1104+
1105+
```js
1106+
/**
1107+
* 102. 二叉树的层序遍历
1108+
* @param {TreeNode} root
1109+
* @return {number[][]}
1110+
*/
1111+
1112+
// 迭代
1113+
1114+
var levelOrder = function(root) {
1115+
const queue = [], res = [];
1116+
root && queue.push(root);
1117+
while(len = queue.length) {
1118+
const val = [];
1119+
while(len--) {
1120+
const node = queue.shift();
1121+
val.push(node.val);
1122+
node.left && queue.push(node.left);
1123+
node.right && queue.push(node.right);
1124+
}
1125+
res.push(val);
1126+
}
1127+
return res;
1128+
};
1129+
1130+
// 递归
1131+
var levelOrder = function(root) {
1132+
const res = [];
1133+
function defs (root, i) {
1134+
if(!root) return;
1135+
if(!res[i]) res[i] = [];
1136+
res[i].push(root.val)
1137+
root.left && defs(root.left, i + 1);
1138+
root.right && defs(root.right, i + 1);
1139+
}
1140+
defs(root, 0);
1141+
return res;
1142+
};
1143+
1144+
1145+
/**
1146+
* 107. 二叉树的层序遍历 II
1147+
* @param {TreeNode} root
1148+
* @return {number[][]}
1149+
*/
1150+
1151+
// 迭代
1152+
1153+
var levelOrderBottom = function(root) {
1154+
const queue = [], res = [];
1155+
root && queue.push(root);
1156+
while(len = queue.length) {
1157+
const val = [];
1158+
while(len--) {
1159+
const node = queue.shift();
1160+
val.push(node.val);
1161+
node.left && queue.push(node.left);
1162+
node.right && queue.push(node.right);
1163+
}
1164+
res.push(val);
1165+
}
1166+
return res.reverse()
1167+
};
1168+
1169+
// 递归
1170+
1171+
var levelOrderBottom = function(root) {
1172+
const res = [];
1173+
function defs (root, i) {
1174+
if(!root) return;
1175+
if(!res[i]) res[i] = [];
1176+
res[i].push(root.val);
1177+
root.left && defs(root.left, i + 1);
1178+
root.right && defs(root.right, i + 1);
1179+
}
1180+
defs(root, 0);
1181+
return res.reverse();
1182+
};
1183+
1184+
/**
1185+
* 199. 二叉树的右视图
1186+
* @param {TreeNode} root
1187+
* @return {number[]}
1188+
*/
1189+
1190+
// 迭代
1191+
1192+
var rightSideView = function(root) {
1193+
const res = [], queue = [];
1194+
root && queue.push(root);
1195+
while(l = queue.length) {
1196+
while (l--) {
1197+
const {val, left, right} = queue.shift();
1198+
!l && res.push(val);
1199+
left && queue.push(left);
1200+
right && queue.push(right);
1201+
}
1202+
}
1203+
return res;
1204+
};
1205+
1206+
// 递归
1207+
var rightSideView = function(root) {
1208+
const res = [];
1209+
function defs(root, i) {
1210+
if(!root) return;
1211+
res[i] = root.val;
1212+
root.left && defs(root.left, i + 1);
1213+
root.right && defs(root.right, i + 1);
1214+
}
1215+
defs(root, 0);
1216+
return res;
1217+
};
1218+
1219+
/**
1220+
* 637. 二叉树的层平均值
1221+
* @param {TreeNode} root
1222+
* @return {number[]}
1223+
*/
1224+
1225+
// 迭代
1226+
var averageOfLevels = function(root) {
1227+
const stack = [], res = [];
1228+
root && stack.push(root);
1229+
while(len = stack.length) {
1230+
let sum = 0, l = len;
1231+
while(l--) {
1232+
const {val, left, right} = stack.shift();
1233+
sum += val;
1234+
left && stack.push(left);
1235+
right && stack.push(right);
1236+
}
1237+
res.push(sum/len);
1238+
}
1239+
return res;
1240+
};
1241+
1242+
// 递归
1243+
var averageOfLevels = function(root) {
1244+
const resCount = [], res = [];
1245+
function defs(root, i) {
1246+
if(!root) return;
1247+
if(isNaN(res[i])) resCount[i] = res[i] = 0;
1248+
res[i] += root.val;
1249+
resCount[i]++;
1250+
root.left && defs(root.left, i + 1);
1251+
root.right && defs(root.right, i + 1);
1252+
}
1253+
defs(root, 0);
1254+
return res.map((val, i) => val / resCount[i]);
1255+
};
1256+
1257+
/**
1258+
* 515. 在每个树行中找最大值
1259+
* @param {TreeNode} root
1260+
* @return {number[]}
1261+
*/
1262+
1263+
// 迭代
1264+
const MIN_G = Number.MIN_SAFE_INTEGER;
1265+
var largestValues = function(root) {
1266+
const stack = [], res = [];
1267+
root && stack.push(root);
1268+
while(len = stack.length) {
1269+
let max = MIN_G;
1270+
while(len--) {
1271+
const {val, left, right} = stack.shift();
1272+
max = max > val ? max : val;
1273+
left && stack.push(left);
1274+
right && stack.push(right);
1275+
}
1276+
res.push(max);
1277+
}
1278+
return res;
1279+
};
1280+
1281+
// 递归
1282+
var largestValues = function(root) {
1283+
const res = [];
1284+
function defs (root, i) {
1285+
if(!root) return;
1286+
if(isNaN(res[i])) res[i] = root.val;
1287+
res[i] = res[i] > root.val ? res[i] : root.val;
1288+
root.left && defs(root.left, i + 1);
1289+
root.right && defs(root.right, i + 1);
1290+
}
1291+
defs(root, 0);
1292+
return res;
1293+
};
1294+
1295+
/**
1296+
* 429. N 叉树的层序遍历
1297+
* @param {Node|null} root
1298+
* @return {number[][]}
1299+
*/
1300+
1301+
// 迭代
1302+
var levelOrder = function(root) {
1303+
const stack = [], res = [];
1304+
root && stack.push(root);
1305+
while(len = stack.length) {
1306+
const vals = [];
1307+
while(len--) {
1308+
const {val, children} = stack.shift();
1309+
vals.push(val);
1310+
for(const e of children) {
1311+
stack.push(e);
1312+
}
1313+
}
1314+
res.push(vals);
1315+
}
1316+
return res;
1317+
};
1318+
1319+
// 递归
1320+
1321+
var levelOrder = function(root) {
1322+
const res = [];
1323+
function defs (root, i) {
1324+
if(!root) return;
1325+
if(!res[i]) res[i] = [];
1326+
res[i].push(root.val);
1327+
for(const e of root.children) {
1328+
defs(e, i + 1);
1329+
}
1330+
}
1331+
defs(root, 0);
1332+
return res;
1333+
};
1334+
1335+
/**
1336+
* 116. 填充每个节点的下一个右侧节点指针
1337+
* 117. 填充每个节点的下一个右侧节点指针 II
1338+
* @param {Node} root
1339+
* @return {Node}
1340+
*/
1341+
1342+
// 迭代
1343+
var connect = function(root) {
1344+
const stack = [];
1345+
root && stack.push(root);
1346+
while(len = stack.length) {
1347+
while(len--) {
1348+
const node1 = stack.shift(),
1349+
node2 = len ? stack[0] : null;
1350+
node1.next = node2;
1351+
node1.left && stack.push(node1.left);
1352+
node1.right && stack.push(node1.right);
1353+
}
1354+
}
1355+
return root;
1356+
};
1357+
1358+
// 递归
1359+
var connect = function(root) {
1360+
const res = [];
1361+
function defs (root, i) {
1362+
if(!root) return;
1363+
if(res[i]) res[i].next = root;
1364+
res[i] = root;
1365+
root.left && defs(root.left, i + 1);
1366+
root.right && defs(root.right, i + 1);
1367+
}
1368+
defs(root, 0);
1369+
return root;
1370+
};
1371+
```
1372+
11031373
-----------------------
11041374
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
11051375
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

0 commit comments

Comments
 (0)