【7.10】算法复习三 二叉树

7.10

  • 23 前序遍历
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) return
    inorder(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) return
    postorder(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) return 
    if(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) return
    if(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 here
    if(!root) return 0
    return 1+Math.max(maxDepth(root.left),maxDepth(root.right))
}
  • 29 和为某一值的路径(根节点到叶子节点)
function hasPathSum( root ,  sum ) {
    // write code here
    if(!root) return false
    if(!root.left && !root.right && root.val == sum) return true
    return hasPathSum(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) return
        inorder(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 here
    if(!pRoot) return true
    function compare(root1,root2){
        if(!root1 && !root2) return true
        if(!root1 || !root2) return false
        if(root1.val !== root2.val) return false
        return compare(root1.left,root2.right) && compare(root1.right,root2.left)
    }
    return compare(pRoot.left,pRoot.right)
}
  • 32 合并二叉树
function mergeTrees( t1 ,  t2 ) {
    // write code here
    if(t1 && t2){
        t1.val += t2.val
        t1.left = mergeTrees(t1.left,t2.left)
        t1.right = mergeTrees(t1.right,t2.right)
    }
    return t1 || t2
}
  • 33 二叉树的镜像
function Mirror( pRoot ) {
    // write code here
    if(!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) return
        inorder(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) return 0
    return 1+Math.max(maxdepth(root.left),maxdepth(root.right))
}
function IsBalanced_Solution(pRoot)
{
    // write code here
    if(!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 here
    if(!root) return 
    if(p<root.val && q<root.val) return lowestCommonAncestor(root.left,p,q)
    if(p>root.val && q>root.val) return lowestCommonAncestor(root.right,p,q)
    return root.val
}
  • 38 二叉树中找到两个节点的最近公共祖先(返回值即可)
function lowestCommonAncestor( root ,  o1 ,  o2 ) {
    // write code here
    if(!root) return
    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 && 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 here
    if(!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 here
    if(s == '') return
    s = s.split('!')
    s.pop()
    let root = new TreeNode(parseInt(s[0]))
    let queue = []
    queue.push(root)
    let i=1
    while(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 here
    if(!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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值