BM23 二叉树的前序遍历
function preorderTraversal( root ) {
let arr=[];
function preOrder(root){
if(!root) return null;
arr.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
preOrder(root)
return arr;
}
module.exports = {
preorderTraversal : preorderTraversal
};
BM24 二叉树的中序遍历
function inorderTraversal( root ) {
let arr=[];
function midOrder(root){
if(!root) return;
midOrder(root.left);
arr.push(root.val);
midOrder(root.right);
}
midOrder(root);
return arr;
}
module.exports = {
inorderTraversal : inorderTraversal
};
BM25 二叉树的后序遍历
function postorderTraversal( root ) {
let arr=[];
function lastOrder(root){
if(!root) return ;
lastOrder(root.left);
lastOrder(root.right);
arr.push(root.val)
}
lastOrder(root)
return arr;
}
module.exports = {
postorderTraversal : postorderTraversal
};
BM26 求二叉树的层序遍历
function levelOrder( root ) {
let arr=[];
function cxOrder(root,level){
if(!root) return;
if(!arr[level]) arr[level]=[];
arr[level].push(root.val);
cxOrder(root.left,level+1);
cxOrder(root.right,level+1);
}
cxOrder(root,0)
return arr;
}
module.exports = {
levelOrder : levelOrder
};
BM27 按之字形顺序打印二叉树
function Print(pRoot)
{
let arr=[];
function cxOrder(root,level){
if(!root) return;
if(!arr[level]) arr[level]=[];
arr[level].push(root.val);
cxOrder(root.left,level+1);
cxOrder(root.right,level+1);
}
cxOrder(pRoot,0);
for(let i=0;i<arr.length;i++){
if(i%2!==0){
arr[i].reverse();
}
}
return arr
}
module.exports = {
Print : Print
};
BM28 二叉树的最大深度
function maxDepth( root ) {
function dfs(root){
if(!root) return 0;
let left=dfs(root.left);
let right=dfs(root.right);
return Math.max(left+1,right+1);
}
return dfs(root);
}
module.exports = {
maxDepth : maxDepth
};
BM29 二叉树中和为某一值的路径(一)
function hasPathSum( root , sum ) {
if(!root) return false;
if(root.val===sum&&!root.left&&!root.right) return true;
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val)
}
module.exports = {
hasPathSum : hasPathSum
};
BM30 二叉搜索树与双向链表
function Convert(pRootOfTree)
{
let head=null,pre=null;
function midOrder(cur){
if(!cur) return;
midOrder(cur.left);
if(pre===null){
head=cur;
}else{
pre.right=cur;
}
cur.left=pre;
pre=cur;
midOrder(cur.right);
}
midOrder(pRootOfTree);
return head;
}
module.exports = {
Convert : Convert
};
BM31 对称的二叉树
function isSymmetrical(pRoot)
{
if(pRoot===null) return true;
function compare(node1,node2){
if(!node1&&!node2) return true;
if(!node1||!node2)return false;
if(node1.val!==node2.val) return false;
return compare(node1.left,node2.right)&&compare(node1.right,node2.left)
}
return compare(pRoot.left,pRoot.right);
}
module.exports = {
isSymmetrical : isSymmetrical
};
BM32 合并二叉树
function mergeTrees( t1 , t2 ) {
if(t1&&t2){
t1.val+=t2.val;
t1.left=mergeTrees(t1.left,t2.left);
t1.right=mergeTrees(t1.right,t2.right);
}
return t1||t2;
}
module.exports = {
mergeTrees : mergeTrees
};
BM33 二叉树的镜像
function Mirror( pRoot ) {
if(!pRoot) return;
let t=pRoot.right;
pRoot.right=Mirror(pRoot.left);
pRoot.left=Mirror(t);
return pRoot;
}
module.exports = {
Mirror : Mirror
};
BM34 判断是不是二叉搜索树
function isValidBST( root ) {
let arr=[];
function midOrder(root){
if(!root) return;
midOrder(root.left);
arr.push(root.val);
midOrder(root.right);
}
midOrder(root);
for(let i=0;i<arr.length-1;i++){
if(arr[i]>arr[i+1])
return false;
}
return true
}
module.exports = {
isValidBST : isValidBST
};
BM35 判断是不是完全二叉树
function isCompleteTree( root ) {
let arr=[];
arr.push(root);
let end=false;
while(arr.length){
let node=arr.shift();
if(!node) end=true;
else{
if(arr.length&&end) return false
arr.push(node.left);
arr.push(node.right);
}
}
return true;
}
module.exports = {
isCompleteTree : isCompleteTree
};
BM36 判断是不是平衡二叉树
function IsBalanced_Solution(pRoot)
{
if(pRoot===null) return true;
if(Math.abs(depth(pRoot.left)-depth(pRoot.right))>1) return false;
return IsBalanced_Solution(pRoot.left)&&IsBalanced_Solution(pRoot.right)
}
function depth(node){
if(node===null){return 0};
let left=depth(node.left);
let right=depth(node.right);
return Math.max(left,right)+1
}
module.exports = {
IsBalanced_Solution : IsBalanced_Solution
};
BM37 二叉搜索树的最近公共祖先
function lowestCommonAncestor( root , p , q ) {
if(!root) return null;
if(p<root.val&&q<root.val)
return lowestCommonAncestor(root.left,p,q);
else if(p>root.val&&q>root.val)
return lowestCommonAncestor(root.right,p,q);
else
return root.val;
}
module.exports = {
lowestCommonAncestor : lowestCommonAncestor
};
BM38 在二叉树中找到两个节点的最近公共祖先
function lowestCommonAncestor( root , o1 , o2 ) {
if(!root) return null;
if(root.val===o1||root.val===o2) return root.val;
let left=lowestCommonAncestor(root.left,o1,o2);
let right=lowestCommonAncestor(root.right,o1,o2);
if(left!==null&&right!==null) return root.val;
return left!==null?left:right;
}
module.exports = {
lowestCommonAncestor : lowestCommonAncestor
};
BM40 重建二叉树
function reConstructBinaryTree(pre, vin)
{
if(!pre.length||!vin.length){
return null;
}
let root=new TreeNode(pre.shift());
let index=vin.indexOf(root.val);
root.left=reConstructBinaryTree(pre, vin.slice(0,index));
root.right=reConstructBinaryTree(pre, vin.slice(index+1,vin.length));
return root;
}
module.exports = {
reConstructBinaryTree : reConstructBinaryTree
};
BM41 输出二叉树的右视图
function solve( xianxu , zhongxu ) {
let level = 0;
let res = [];
function rebuild(xianxu, zhongxu, level, res) {
if(xianxu.length === 0) return null;
const root = xianxu[0];
const index = zhongxu.findIndex(node => node === root)
const leftNodePreOrder = xianxu.slice(1, index + 1);
const leftNodeInOrder = zhongxu.slice(0, index);
const rightNodePreOrder = xianxu.slice(index + 1);
const rightNodeInOrder = zhongxu.slice(index + 1);
rebuild(leftNodePreOrder, leftNodeInOrder, level + 1, res);
rebuild(rightNodePreOrder, rightNodeInOrder, level + 1, res);
res[level] = root;
}
rebuild(xianxu, zhongxu, level, res);
return res;
}
module.exports = {
solve : solve
};