function preorder(root,arr){if(!root)return
arr.push(root.val)preorder(root.left,arr)preorder(root.right,arr)}
function preorderTraversal( root ){// write code here
let arr =[]preorder(root,arr)return arr
}
24 中序遍历
function inorder(root,arr){if(!root)returninorder(root.left,arr)
arr.push(root.val)inorder(root.right,arr)}
function inorderTraversal( root ){// write code here
let arr =[]inorder(root,arr)return arr
}
25 后序遍历
function postorder(root,arr){if(!root)returnpostorder(root.left,arr)postorder(root.right,arr)
arr.push(root.val)}
function postorderTraversal( root ){// write code here
let arr =[]postorder(root,arr)return arr
}
26 层序遍历
function preorder(root,level,arr){if(!root)returnif(level >= arr.length) arr.push([])
arr[level].push(root.val)preorder(root.left,level+1,arr)preorder(root.right,level+1,arr)}
function levelOrder( root ){// write code here
let arr =[]preorder(root,0,arr)return arr
}
27 之字型顺序打印
function preorder(root,level,arr){if(!root)returnif(level >= arr.length) arr.push([])if(level %2){
arr[level].unshift(root.val)}else{
arr[level].push(root.val)}preorder(root.left,level+1,arr)preorder(root.right,level+1,arr)}
function Print(pRoot){// write code here
let arr =[]preorder(pRoot,0,arr)return arr
}
28 最大深度
function maxDepth( root ){// write code hereif(!root)return0return1+Math.max(maxDepth(root.left),maxDepth(root.right))}
29 和为某一值的路径(根节点到叶子节点)
function hasPathSum( root , sum ){// write code hereif(!root)return false
if(!root.left &&!root.right && root.val == sum)return true
returnhasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val)}
30 将二叉搜索树变为双向链表
function Convert(pRootOfTree){// write code here
let head = null,pre = null
function inorder(root){if(!root)returninorder(root.left)if(!pre){
head = root
}else{
pre.right = root
}
root.left = pre
pre = root
inorder(root.right)}inorder(pRootOfTree)return head
}
31 判断二叉树是否对称
function isSymmetrical(pRoot){// write code hereif(!pRoot)return true
function compare(root1,root2){if(!root1 &&!root2)return true
if(!root1 ||!root2)return false
if(root1.val !== root2.val)return false
returncompare(root1.left,root2.right)&&compare(root1.right,root2.left)}returncompare(pRoot.left,pRoot.right)}
function Mirror( pRoot ){// write code hereif(!pRoot)return[pRoot.left,pRoot.right]=[pRoot.right,pRoot.left]Mirror(pRoot.left)Mirror(pRoot.right)return pRoot
}
34 判断是不是二叉搜索树
function isValidBST( root ){// write code here
let arr =[]
function inorder(root){if(!root)returninorder(root.left)
arr.push(root.val)inorder(root.right)}inorder(root)for(let i =0;i<arr.length-1;i++){if(arr[i]>=arr[i+1])return false
}return true
}
35 判断是不是完全二叉树 【有点抽象】
function isCompleteTree( root ){// write code here
let arr =[]
arr.push(root)
let flag = false
while(arr.length){
let node = arr.shift()if(!node){
flag = true
}else{if(arr.length && flag){return false
}
arr.push(node.left)
arr.push(node.right)}}return true
}
36 判断是不是平衡二叉树(只考虑平衡性)
function maxdepth(root){if(!root)return0return1+Math.max(maxdepth(root.left),maxdepth(root.right))}
function IsBalanced_Solution(pRoot){// write code hereif(!pRoot)return true
return Math.abs(maxdepth(pRoot.left)-maxdepth(pRoot.right))<=1&&IsBalanced_Solution(pRoot.left)&&IsBalanced_Solution(pRoot.right)}
37 二叉搜索树的最近公共祖先(返回值即可)
function lowestCommonAncestor( root , p , q ){// write code hereif(!root)returnif(p<root.val && q<root.val)returnlowestCommonAncestor(root.left,p,q)if(p>root.val && q>root.val)returnlowestCommonAncestor(root.right,p,q)return root.val
}
38 二叉树中找到两个节点的最近公共祖先(返回值即可)
function lowestCommonAncestor( root , o1 , o2 ){// write code hereif(!root)returnif(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 && right)return root.val
return left || right
}
39 序列化二叉树【难 要写非递归的层序遍历】
function TreeNode(x){
this.val = x;
this.left = null;
this.right = null;}
function Serialize(pRoot){// write code hereif(!pRoot)return''
let str =''
let queue =[]
queue.push(pRoot)
str = str + pRoot.val +'!'while(queue.length){
let node = queue.shift()if(node.left){
str = str+node.left.val+'!'
queue.push(node.left)}else{
str = str +'#!'}if(node.right){
str = str+node.right.val+'!'
queue.push(node.right)}else{
str = str +'#!'}}return str
}
function Deserialize(s){// write code hereif(s =='')return
s = s.split('!')
s.pop()
let root = new TreeNode(parseInt(s[0]))
let queue =[]
queue.push(root)
let i=1while(i<s.length){
let node = queue.shift()if(s[i]!=='#'){
node.left = new TreeNode(parseInt(s[i]))
queue.push(node.left)}
i++if(s[i]!=='#'){
node.right = new TreeNode(parseInt(s[i]))
queue.push(node.right)}
i++}return root
}
40 根据前序和中序遍历重建二叉树
function reConstructBinaryTree(pre, vin){// write code hereif(!vin.length)return
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))return root
}
41 根据前序和中序遍历输出二叉树的右视图 【有点难】
function solve( xianxu , zhongxu ){// write code here
let level =0,arr =[]
function reConstructBinaryTree(pre,vin,level){if(!vin.length)return
let root = new TreeNode(pre.shift())
let index = vin.indexOf(root.val)
root.left =reConstructBinaryTree(pre,vin.slice(0,index),level+1)
root.right =reConstructBinaryTree(pre,vin.slice(index+1),level+1)
arr[level]= root.val
return root
}reConstructBinaryTree(xianxu,zhongxu,level)return arr
}