- 二叉树相关算法总结
1、先序遍历
2、中序遍历
3、后序遍历
4、相同的树
5、翻转二叉树
6、二叉搜索树的第k小的元素
7、二叉树的层序遍历
8、二叉树的最大深度
9、N叉树的先序遍历
10、二叉树的所有路径
11、平衡二叉树( -这里是具体的题目思路描述)
12、二叉树的直径
//二叉树遍历
//先序遍历
const preOrderTraversal = function(root){
let res = []
function traversal (root){
if(root!==null){
res.push(root.val)
traversal(root.left)
traversal(root.right)
}
}
traversal(root)
return res
}
//中序遍历
const midOrderTraversal = function(root){
let res = []
function traversal (root){
if(root!==null){
traversal(root.left)
res.push(root.val)
traversal(root.right)
}
}
traversal(root)
return res
}
//后序遍历
const postOrderTraversal = function(root){
let res = []
function traversal (root){
if(root!==null){
traversal(root.left)
traversal(root.right)
res.push(root.val)
}
}
traversal(root)
return res
}
//相同的树
const sameTree = function(p,q){
function traversal(p,q){
if(p!==null&&q===null){
return false
}
else if(p===null && q !== null){
return false
}
else if(p===null && q===null){
return true
}
else{
return p.val===q.val&&traversal(p.left,q.right)&& traversal(p.right,q.left)
}
}
return traversal(p,q)
}
//翻转二叉树
const reverse = function(root){
function traversal(root){
if(root=== null) return
else{
[root.left,root.right] = [traversal(root.right),traversal(root.left)]
return root
}
}
traversal(root)
}
//二叉搜索树的第k小的元素
const kthSmallest = function(root,k){
let res = []
function traversal(root){
if(!root&& res.length < k){
traversal(root.left)
res.push(root.val)
traversal(root.right)
}
}
traversal(root)
return res[k-1]
}
//二叉树的层序遍历
const OrderTraversal = function(root){
let res = []
function traversal(root,depth){
if(!root){
if(!res[depth]){
res[depth] = []
}
else{
traversal(root.left, depth + 1)
res[depth].push(root.val)
traversal(root.right, depth + 1)
}
}
}
traversal(root,0)
return res
}
//二叉树的最大深度
var maxDepth = function (root) {
let res = 0
function traversal (root, depth) {
if (root !== null) {
if (depth > res) {
res = depth
}
if (root.left) {
traversal(root.left, depth + 1)
}
if (root.right) {
traversal(root.right, depth + 1)
}
}
}
traversal(root, 1)
return res
}
//N叉树的先序遍历
const preNRoot = function(root){
let res = []
function traversal(root){
res.push(root.val)
root.children.forEach(child=>traversal(child))
}
traversal(root)
return root
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {string[]}
*/
var binaryTreePaths = function(root) {
const paths = [];
const traversal = (root, path) => {
if (root) {
path += root.val;
if (root.left === null && root.right === null) { // 当前节点是叶子节点
paths.push(path); // 把路径加入到答案中
} else {
path += "->"; // 当前节点不是叶子节点,继续递归遍历
traversal(root.left, path);
traversal(root.right, path);
}
}
}
traversal(root, "");
return paths;
};
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
return height(root)>=0
};
var height = (root)=>{
if(root == null ){
return 0
}
let lr = height(root.left)
let rr = height(root.right)
console.log(lr,rr)
if(lr>=0&&rr>=0&&Math.abs(lr-rr)<=1){
return Math.max(lr,rr)+1
}
else{
return -1
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var diameterOfBinaryTree = function(root) {
// 默认为1是因为默认了根节点自身的路径长度
let ans = 1;
function depth(rootNode) {
if (!rootNode) {
// 如果不存在根节点,则深度为0
return 0;
}
// 递归,获取左子树的深度
let L = depth(rootNode.left);
// 递归,获取右子树的深度
let R = depth(rootNode.right);
/* 关键点1
L+R+1的公式是如何而来?
等同于:左子树深度(节点个数) + 右子树深度(节点个数) + 1个根节点
便是这株二叉树从最左侧叶子节点到最右侧叶子节点的最长路径
类似于平衡二叉树的最小值节点到最大值节点的最长路径
之所以+1是因为需要经过根节点
*/
// 获取该树的最长路径和现有最长路径中最大的那个
ans = Math.max(ans, L + R + 1);
/* 关键点2
已知根节点的左右子树的深度,
则,左右子树深度的最大值 + 1,
便是以根节点为数的最大深度*/
return Math.max(L, R) + 1;
}
depth(root);
// 由于depth函数中已经默认加上数节点的自身根节点路径了,故此处需减1
return ans - 1;
};