算法基础之二叉树
来源:互联网 发布:java.nio.file.path 编辑:程序博客网 时间:2024/04/30 01:16
本文主要包括树相关的算法,二叉树结点基本结构如下
function TreeNode(x) { this.val = x; this.left = null; this.right = null;}
本文还会继续更新。
二叉树的深度
function depth(pRoot){ if(!pRoot){ return 0; } var depth = 0; var currDepth = 0; dfs(pRoot); return depth; function dfs(node){ if(!node){ depth = depth > currDepth ? depth : currDepth; return; } currDepth++; dfs(node.left); dfs(node.right); currDepth--; }}
二叉树的前序遍历
function preOrder(root){ if(!root) return []; var result = []; _preOrder(root); return result; function _preOrder(node){ result.push(node.val); node.left && _preOrder(node.left); node.right && _preOrder(node.right); }}
二叉树的中序遍历
function inOrder(root){ if(!root) return []; var result = []; _inOrder(root); return result; function _inOrder(node){ node.left && _inOrder(node.left); result.push(node.val); node.right && _inOrder(node.right); }}
二叉树的后序遍历
function postOrder(root){ if(!root) return []; var result = []; _postOrder(root); return result; function _postOrder(node){ node.left && _postOrder(node.left); node.right && _postOrder(node.right); result.push(node.val); }}
二叉树的层序遍历
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null;} */function printFromTopToBottom(root){ if (!root) { return []; } var queue = []; queue.push(root); var result = []; while (queue.length) { var temp = queue.shift(); result.push(temp.val); if (temp.left) { queue.push(temp.left); } if (temp.right) { queue.push(temp.right); } } return result;}
根据层序遍历建立二叉树
//层序的空节点使用 nullfunction Tree(arr, node, num){ //也可以通过 new 调用 if(!arr || arr.length === 0){ return new TreeNode(null); } num = num || 1; node = node || new TreeNode(arr[num - 1]); var curr = node; if(num * 2 - 1 < arr.length && arr[num * 2 - 1] != null){ curr.left = new TreeNode(arr[num * 2 - 1]); Tree(arr, curr.left, num * 2); } if(num * 2 < arr.length && arr[num * 2] != null){ curr.right = new TreeNode(arr[num * 2]); Tree(arr, curr.right, num * 2 + 1); } return node;}
根据中序遍历和前序遍历重建二叉树
function reBuildTree_pi(pre, vin){ if(pre.length == 0 || vin.length == 0 || pre.length !== vin.length){ return null; }; var index = vin.indexOf(pre[0]); var left = vin.slice(0,index); var right = vin.slice(index+1); var node = new TreeNode(vin[index]); node.left = reBuildTree_pi(pre.slice(1,index+1),left); node.right = reBuildTree_pi(pre.slice(index+1),right); return node;}
根据中序遍历和后序遍历重建二叉树
function reBuildTree_ip(vin, post){ if(post.length == 0 || vin.length == 0 || vin.length !== post.length){ return null; }; var index = vin.indexOf(post.pop()); var left = vin.slice(0,index); var right = vin.slice(index+1); var node = new TreeNode(vin[index]); node.left = reBuildTree_ip(left, post.slice(0,index)); node.right = reBuildTree_ip(right, post.slice(index)); return node;}
求二叉树的最远节点距离
function maxDistance(root){ //root 二叉树根节点; if(root === null) return 0; var max = 0; _maxDistance(root, max); return max; //函数返回最大值 function _maxDistance(curr){ //curr: 当前节点;max: 最大值; if(curr === null) return 0; var leftDepth = curr.left ? _maxDistance(curr.left) : 0; var rightDepth = curr.right ? _maxDistance(curr.right) : 0; if(leftDepth + rightDepth > max) max = leftDepth + rightDepth; return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; }}
二叉树的镜像
function mirror(root){ if(!root){ return null; } var temp = root.left; root.left = root.right; root.right = temp; if(root.left){ Mirror(root.left); } if(root.right){ Mirror(root.right); }}
二叉搜索树转双向链表
将左子树构成双向链表,返回的是左子树的尾结点,将其连接到root的左边;
将右子树构成双向链表,将其追加到root结点之后,并返回尾结点;
向左遍历返回的链表至头结点处,即为所求双向链表的首结点。
function convert(pRootOfTree){ if(!pRootOfTree) { return null; } var lastNode = null; lastNode = ConvertNode(pRootOfTree); var head = lastNode; while(head && head.left) { head = head.left; } return head; function ConvertNode(node){ if(!node){ return; } if(node.left) { lastNode = ConvertNode(node.left); } node.left = lastNode; if(lastNode){ lastNode.right = node; } lastNode = node; if(node.right){ lastNode = ConvertNode(node.right); } return lastNode; }}
判断是否平衡二叉树
function isBalancedTree(pRoot){ if(!pRoot){ return true; } var left = TreeDepth(pRoot.left); var right = TreeDepth(pRoot.right); var diff = left - right; if(diff > 1 || diff < -1) return false; return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right); function TreeDepth(pRoot){ if(!pRoot){ return 0; } var depth = 0; var currDepth = 0; dfs(pRoot); return depth; function dfs(node){ if(!node){ depth = depth > currDepth ? depth : currDepth; return; } currDepth++; dfs(node.left); dfs(node.right); currDepth--; } }}
判断是否对称二叉树
function isSymmetrical(pRoot){ if(!pRoot){ return true; } return symmetrical(pRoot, pRoot); function symmetrical(node1,node2){ if(!node1 && !node2) return true; if(!node1 || !node2) return false; if(node1.val != node2.val) return false; return symmetrical(node1.left, node2.right) && symmetrical(node1.right, node2.left); }}
判断是否完全二叉树
function isPrefectTree(root){ if(!root) return true; var que = []; que.push(root); var curr; while(curr = que.shift()){ que.push(curr.left); que.push(curr.right); } while (que.length > 0){ if (que.pop()) return false; } return true;}
判断是否满二叉树
function isFullTree(root){ if(!root) return true; var que = []; que.push(root); var curr; var nodeNum = 0; while(curr = que.shift()){ que.push(curr.left); que.push(curr.right); nodeNum++; } while (que.length > 0){ if (que.pop()) return false; } return (nodeNum & (nodeNum + 1)) === 0;}
阅读全文
0 0
- 算法基础之二叉树
- 基础查找算法之二叉查找算法
- 二叉树基础算法总结
- 【基础算法】:线索二叉树
- 数据结构基础算法-二叉树
- 基础入门之二叉树
- 算法之二叉查找树
- 算法练习之二叉树
- 算法04 之二叉树
- C++算法之二叉树
- 算法基础---数据结构---树和二叉树
- 算法笔记:树和二叉树基础
- 二叉树的一些基础算法
- .net 数据结构与算法基础:二叉树
- 基础算法001---二叉树遍历 Java
- 【基础算法】:平衡二叉树的插入
- 常见二叉树基础算法汇总
- C语言基础 16 二叉树 算法
- spring中@order
- 《Spring技术内幕》学习笔记14——Spring读取和处理事务配置
- 从2017年顶会论文看Attention Model
- 知识图谱前沿技术课程(华东师范大学站)
- proc文件系统创建实例三( seq file 文件系统的创建过程,非结构化信息显示)
- 算法基础之二叉树
- Linux内核概述——之一
- 本地eclipse用相对路径创建文件路径问题
- ios端页面滚动出现卡顿的原因之一
- 字符转ASCII
- Android之Kotlin里使用单元测试
- 关于HashMap那些事
- java通信实现
- 实现ScrollView头标题滑动渐变