mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
@ -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 queue = [], res = [];
|
||||||
|
root && queue.push(root);
|
||||||
|
while(len = queue.length) {
|
||||||
|
let sum = 0, l = len;
|
||||||
|
while(l--) {
|
||||||
|
const {val, left, right} = queue.shift();
|
||||||
|
sum += val;
|
||||||
|
left && queue.push(left);
|
||||||
|
right && queue.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 queue = [], res = [];
|
||||||
|
root && queue.push(root);
|
||||||
|
while(len = queue.length) {
|
||||||
|
let max = MIN_G;
|
||||||
|
while(len--) {
|
||||||
|
const {val, left, right} = queue.shift();
|
||||||
|
max = max > val ? max : val;
|
||||||
|
left && queue.push(left);
|
||||||
|
right && queue.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 queue = [], res = [];
|
||||||
|
root && queue.push(root);
|
||||||
|
while(len = queue.length) {
|
||||||
|
const vals = [];
|
||||||
|
while(len--) {
|
||||||
|
const {val, children} = queue.shift();
|
||||||
|
vals.push(val);
|
||||||
|
for(const e of children) {
|
||||||
|
queue.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 queue = [];
|
||||||
|
root && queue.push(root);
|
||||||
|
while(len = queue.length) {
|
||||||
|
while(len--) {
|
||||||
|
const node1 = queue.shift(),
|
||||||
|
node2 = len ? queue[0] : null;
|
||||||
|
node1.next = node2;
|
||||||
|
node1.left && queue.push(node1.left);
|
||||||
|
node1.right && queue.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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
@ -374,6 +374,86 @@ func postorderTraversal(root *TreeNode) []int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
javaScript:
|
||||||
|
|
||||||
|
> 前序遍历统一迭代法
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// 前序遍历:中左右
|
||||||
|
// 压栈顺序:右左中
|
||||||
|
|
||||||
|
var preorderTraversal = function(root, res = []) {
|
||||||
|
const stack = [];
|
||||||
|
if (root) stack.push(root);
|
||||||
|
while(stack.length) {
|
||||||
|
const node = stack.pop();
|
||||||
|
if(!node) {
|
||||||
|
res.push(stack.pop().val);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node.right) stack.push(node.right); // 右
|
||||||
|
if (node.left) stack.push(node.left); // 左
|
||||||
|
stack.push(node); // 中
|
||||||
|
stack.push(null);
|
||||||
|
};
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
> 中序遍历统一迭代法
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// 中序遍历:左中右
|
||||||
|
// 压栈顺序:右中左
|
||||||
|
|
||||||
|
var inorderTraversal = function(root, res = []) {
|
||||||
|
const stack = [];
|
||||||
|
if (root) stack.push(root);
|
||||||
|
while(stack.length) {
|
||||||
|
const node = stack.pop();
|
||||||
|
if(!node) {
|
||||||
|
res.push(stack.pop().val);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (node.right) stack.push(node.right); // 右
|
||||||
|
stack.push(node); // 中
|
||||||
|
stack.push(null);
|
||||||
|
if (node.left) stack.push(node.left); // 左
|
||||||
|
};
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
> 后序遍历统一迭代法
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// 后续遍历:左右中
|
||||||
|
// 压栈顺序:中右左
|
||||||
|
|
||||||
|
var postorderTraversal = function(root, res = []) {
|
||||||
|
const stack = [];
|
||||||
|
if (root) stack.push(root);
|
||||||
|
while(stack.length) {
|
||||||
|
const node = stack.pop();
|
||||||
|
if(!node) {
|
||||||
|
res.push(stack.pop().val);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
stack.push(node); // 中
|
||||||
|
stack.push(null);
|
||||||
|
if (node.right) stack.push(node.right); // 右
|
||||||
|
if (node.left) stack.push(node.left); // 左
|
||||||
|
};
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user