二叉树常考题型总结
对于二叉树的题型,多出在面试中,笔试相对较少,要多总结做到心中有数。尽量做到递归和迭代都熟悉。
常考题型
1、数组转树、树转数组
function arrayToTree(array){
let root = array[0];
array.shift();
let tree = {
id:root.id,
val:root.val,
children:array.length > 0 ? toTree(root.id,array):[]
}
return tree;
}
function toTree(parentId,array){
let children = [];
let len = array.length;
for(let i = 0; i < len; i++){
let node = array[i];
if(node.parentId === parentId){
children.push({
id:node.id,
val:node.val,
children:toTree(node.id,array)
})
}
}
return children;
}
2、二叉树的所有路径,路径和之类(含有回溯思想)
var hasPathSum = function(root, targetSum) {
if(!root) return false;
let result = false;
let res = root.val;
function backtracking(node,sum){
if(!node.left && !node.right){
if(res == sum){
result = true;
}
return;
}
if(node.left){
res = res+node.left.val;
backtracking(node.left,sum);
res = res - node.left.val;
}
if(node.right){
res = res+node.right.val;
backtracking(node.right,sum);
res = res - node.right.val;
}
}
backtracking(root,targetSum);
return result;
};
var pathSum = function(root, targetSum) {
let result = [];
let path = [];
if(root === null) return [];
let sum = root.val;
path.push(root.val);
function findpath(node){
if(!node.left && !node.right){
if(sum === targetSum){
result.push([...path]);
}
return ;
}
if(node.left){
sum += node.left.val;
path.push(node.left.val);
findpath(node.left);
sum -= node.left.val;
path.pop()
}
if(node.right){
sum += node.right.val;
path.push(node.right.val);
findpath(node.right);
sum -= node.right.val;
path.pop();
}
}
findpath(root);
return result;
};
3、二叉搜索树排序,查找
中序遍历下二叉搜索树是有序的
4、前中序构建后序
lc105前序中序构造树
var buildTree = function(preorder, inorder) {
if(!preorder.length) return null;
const rootval = preorder.shift();//获取到中间节点
const index = inorder.indexOf(rootval);
const root = new TreeNode(rootval);
root.left = buildTree(preorder.slice(0,index),inorder.slice(0,index));
root.right = buildTree(preorder.slice(index),inorder.slice(index+1));
return root;
};
lc106中序和后序构建树
var buildTree = function(inorder, postorder) {
//中序与后序区别与中序与前序就是,中间节点是从后往前的
if(!inorder.length) return null;
let rootval = postorder.pop();
let root = new TreeNode(rootval);
let index = inorder.indexOf(rootval);
root.left = buildTree(inorder.slice(0,index),postorder.slice(0,index));
root.right = buildTree(inorder.slice(index+1),postorder.slice(index));
return root;
};
5、层序遍历
6、 递归
练习题目
lc226 翻转二叉树(简单)
如果有子节点通过递归不断的把底层的交换后,再进行上层节点的交换(递归法很好理解)
var invertTree = function(root) {
//翻转二叉树,递归法
if(!root) return null;
let temp = root.right;
root.right = invertTree(root.left);
root.left = invertTree(temp);
return root;
};
迭代法如何实现?其实很简单,树都是数组在该填下一层的之前将进行转换就行了,这样就是先了整个树的转换,不过与递归不同的是,先转换上层,递归先转换的是下层。
var invertTree = function(root) {
//写个层序遍历的方法
//先写个节点交换函数
function swap(node,left,right){
let temp = right;
right = left;
left = temp;
node.left = left;
node.right = right;
}
let queue = [];
if(!root) return root;
queue.push(root);
while(queue.length){
let len = queue.length;
for(let i = 0; i < len; i++){
let node = queue.shift();
swap(node,node.left,node.right);
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
}
return root;
};
lc101对称二叉树(简单)
递归法
var isSymmetric = function(root) {
//使用递归遍历左右子树 递归三部曲
// 1. 确定递归的参数 root.left root.right和返回值true false
const compareNode=function(left,right){
//2. 确定终止条件 空的情况
if(left===null&&right!==null||left!==null&&right===null){
return false;
}else if(left===null&&right===null){
return true;
}else if(left.val!==right.val){
return false;
}
//3. 确定单层递归逻辑
let outSide=compareNode(left.left,right.right);
let inSide=compareNode(left.right,right.left);
return outSide&&inSide;
}
if(root===null){
return true;
}
return compareNode(root.left,root.right);
};
迭代法:这里不一样的是,每次取出两个元素进行比较,值得注意
var isSymmetric = function(root) {
//迭代法
//再加入元素前先检查元素是否对称
let queue = [];
if(root == null) return true;
queue.push(root.left);
queue.push(root.right)
while(queue.length){
let leftnode = queue.shift();
let rightnode = queue.shift();
if(leftnode === null && rightnode === null){
continue;
}
if(leftnode === null || rightnode === null || leftnode.val != rightnode.val){
return false;
}
queue.push(leftnode.left);
queue.push(rightnode.right);
queue.push(leftnode.right);
queue.push(rightnode.left);
}
return true;
};
lc104二叉树最大深度(简单)
递归法
var maxDepth = function(root) {
function getdepth(node){
if(node === null) return 0;
const leftdepth = getdepth(node.left);
const rightdepth = getdepth(node.right);
return 1+Math.max(leftdepth,rightdepth);
}
if(root === null) return 0;
return getdepth(root);
};
层序遍历也可以实现哈
var maxDepth = function(root) {
//层序遍历来做,这个我最熟悉了哈
if(root == null) return 0;
let queue = [];
queue.push(root);
let count = 0;
while(queue.length){
let len = queue.length;
for(let i = 0; i < len; i++){
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
count++;
}
return count;
};
lc110平衡二叉树(简单)
var isBalanced = function(root) {
const getdepth = function(node){
if(node === null) return 0;
let leftdepth = getdepth(node.left);
if(leftdepth === -1) return -1;
let rightdepth = getdepth(node.right);
if(rightdepth === -1) return -1;
if(Math.abs(leftdepth-rightdepth) > 1){
return -1;
}else{
return 1+Math.max(leftdepth,rightdepth);
}
}
return !(getdepth(root) === -1)
};