这里优先选择了 LeetCode 热题 HOT 100 中的树题,毕竟刷题的边际收益就是冲击需要算法的面试,所以 Hot 优先级更高。
递归遍历
迭代遍历 -- 双色标记法
按照那个男人的指示,正常我们就用递归做就好,就好像我们做非排序题排序的时候,sort 一下就好了,但是一旦面试官问到用另外的迭代方式的时候,我们再套个模板,会比记住多个迭代写法要简单,毕竟内存容量有限,而后续遍历的迭代写法确实挺坑的,能省一点内存就省一点吧
// 144. 二叉树的前序遍历
/** * @分析 -- 递归 */
var preorderTraversal = function (root) {
const ret = [];
const recursion = (root) => {
if (!root) return;
ret.push(root.val);
recursion(root.left);
recursion(root.right);
};
recursion(root);
return ret;
};
/** * @分析 -- 迭代 -- 双色标记法 * 1. 使用颜色标记节点状态,新节点为白色,已经访问的节点为灰色 -- 可以用数字或者其他任意标签标示 * 2. 如果遇到的节点是白色,则标记为灰色,然后将右节点,自身,左节点一次入栈 -- 中序遍历 * 3. 如果遇到的节点是灰色的,则将节点输出 * 4. 注意这里是用 stack 栈来存储的,所以是后进先出,这里是前序遍历,中 - 左 - 右 ,那么在插入栈的时候要反过来 右 - 左 - 中 */
var preorderTraversal = function (root) {
const ret = [];
const stack = [];
stack.push([root, 0]); // 0 是白色未处理的,1 是灰色处理过的
while (stack.length) {
const [root, color] = stack.pop();
if (root) {
if (color === 0) {
// 遇到白球,则插入 -- 前序
stack.push([root.right, 0]);
stack.push([root.left, 0]);
stack.push([root, 1]);
} else {
// 遇到灰球,则收网
ret.push(root.val);
}
}
}
return ret;
};
// 94. 二叉树的中序遍历
/** * @分析 * 1. 递归的时候前中后序都能直接处理完了 * 2. 递归是前中后序遍历最简单也是最容易出理解的方法,不懂的画个图就好了 */
var inorderTraversal = function(root) {
const ret = []
const recursion = root => {
if(!root) return
recursion(root.left)
// 这里是中序,所以在两个递归之间,如果是前序就在前面,后序就在后面
ret.push(root.val)
recursion(root.right)
}
recursion(root)
return ret
};
/** * @分析 -- 迭代 -- 双色标记法 * 1. 使用颜色标记节点状态,新节点为白色,已经访问的节点为灰色 -- 可以用数字或者其他任意标签标示 * 2. 如果遇到的节点是白色,则标记为灰色,然后将右节点,自身,左节点一次入栈 -- 中序遍历 * 3. 如果遇到的节点是灰色的,则将节点输出 * 4. 注意这里是用 stack 栈来存储的,所以是后进先出,所以如果是中序遍历,左 - 中 - 右 ,那么在插入栈的时候要反过来 右 - 中 - 左 */
var inorderTraversal = function(root) {
const ret = []
const stack = []
stack.push([root,0]) // 0 是白色未处理的,1 是灰色处理过的
while(stack.length) {
const [root,color] = stack.pop()
if(root){
if(color === 0){
// 遇到白球,则插入 -- 中序遍历
stack.push([root.right,0])
stack.push([root,1])
stack.push([root.left,0])
}else{
// 遇到灰球,则收网
ret.push(root.val)
}
}
}
return ret
};
// 145. 二叉树的后序遍历
/** * @分析 -- 递归 */
var postorderTraversal = function(root) {
const ret = []
const dfs = (root) => {
if(!root) return
dfs(root.left)
dfs(root.right)
ret.push(root.val)
}
dfs(root)
return ret
};
/** * @分析 -- 迭代 -- 双色球 */
var postorderTraversal = function(root) {
const ret = []
const stack = []
stack.push([root,0])
while(stack.length){
const [root,color] = stack.pop()
if(root) {
if(color === 0){
stack.push([root,1])
stack.push([root.right,0])
stack.push([root.left,0])
}else{
ret.push(root.val)
}
}
}
return ret
}
参考视频:传送门
// 101. 对称二叉树
var isSymmetric = function (root) {
if (!root) return false;
const dfs = (left, right) => {
if (!left && !right) return true;
if (!left || !right || left.val !== right.val) return false;
return dfs(left.left, right.right) && dfs(left.right, right.left);
};
return dfs(root.left, root.right);
};
// 104. 二叉树的最大深度
/** * 1.无论是深度,层数等,直接用层序遍历找到最后一层的最后一个叶子节点即可 */
var maxDepth = function(root) {
if(!root) return 0
let ret = 0
const queue = []
queue.push(root)
while(queue.length){
ret++ // 进入一层
let len = queue.length
while(len--){
// 层序遍历
const root = queue.shift()
if(root.left) queue.push(root.left)
if(root.right) queue.push(root.right)
}
}
return ret
}
/** * 1. 自顶向上,带个层数参数,判定为叶子节点就进行最大值判断 */
var maxDepth = function (root) {
if(!root) return 0
let ret = 0;
const dfs = (root, depth) => {
if (root.left) dfs(root.left, depth + 1);
if (root.right) dfs(root.right, depth + 1);
// 走到这的时候,证明是叶子节点了,所以取最大值,就结束这一次的
ret = Math.max(ret, depth);
};
dfs(root, 1);
return ret;
};
// 自低向上
var maxDepth = function (root) {
const recursion = (root) => {
// 只是到了底部,所以高度为 0
if (!root) return 0;
// 每一个节点的高度是多少,就是两个节点树的最大高度+自己所处的这一层1
return Math.max(recursion(root.left), recursion(root.right))+1;
};
return recursion(root);
};
// 226. 翻转二叉树
var invertTree = function (root) {
const dfs = (root) => {
// 到达了最底部,直接返回 null
if (!root) return null;
// 1.递归获取翻转后的左右子树
const left = dfs(root.left)
const right = dfs(root.right)
// 2.反转两棵树的位置
root.left = right
root.right = left
// 最后返回这个反转之后的树
return root;
};
return dfs(root);
};
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。