js矩阵

螺旋矩阵:

export default(arr)=>{
  //处理每一圈的数据遍历过程
  let map=(arr,r=[])=>{
    for(let i=0,len=arr.length;i<len;i++){
      if(i===0){
        r=r.concat(arr[i])
      }else if(i===len-1){
        r=r.concat(arr[i],reverse())
      }else{
        r.push(arr[i].pop())
      }
    }
    arr.shift()
    arr.pop()
    for(let i=arr.length-1;i>=0;i--){
       r.push(arr[i].shift())
    }
    if(arr.length){
      return map(arr,r)
    }else{
      return r
    }
  }
  return map(arr,[])
}

  

旋转图像:

 

 

export default(arr)=>{
  // 获取n的维度
  let vecor=arr.length
  //垂直反转
  for(let i=0,len=vecor/2;i<len;i++){
    for(let j=0,tmp;j<vecor;j++){
       tmp=arr[i][j]
       arr[i][j]=arr[vecor-i-1][j]
       arr[vecor-i-1][j]=tmp
    }
  }
  //对角线翻转
  for(let i=0;i<vecor;i++){
    for(let j=0,tmp;j<i;j++){
      tmp=arr[i][j]
      arr[i][j]=arr[j][i]
      arr[j][i]=tmp
    }
  }
  return arr
}

  二叉树:

对称的二叉树:

 

  //二叉树的节点
  class Node{
    constructor(val){
      this.val=val
      this.left=this.right=undefined
    }
  }
  class Tree{
    constructor(data){
      //临时存储所有节点,方便寻找父子节点
      let nodeList=[]
      //顶节点
      let root
      for(let i=0,len=data.length;i<len;i++){
        let node=new Node(data[i])
        nodeList.push(node)
        if(i>0){  //第一层
          //计算当前节点属于哪一层
          let n=Math.floor( Math.sqrt(i+1))
          //记录当前层的起始点
          let q=Math.pow(2,n)-1
          //记录上一层的起始点
          let p=Math.pow(2,n-1)-1
          //找到当前节点的父节点
          let parent=nodeList[p+Math.floor((i-q)/2)]
          //将当前节点和上一层的父节点做关联
          if(parent.left){
            parent.right=node
          }else{
            parent.left=node
          }
        }
      }
      root=nodeList.shift()
      nodeList.length=0 
      return root
    }
    //判断是不是对称
    static isSymmetry(root){
      if(!root){
        return true
      }
      let walk=(left,right)=>{
        if(!left&&!right){
          return true
        }
        if((left&&!right)||(!left&&right)||(left.val!==right.val)){
          return false
        }
        return walk(left.left,right.right)&&walk(left.right,right.left)
      }
      return walk(root.left,root.right)
    }
  }

  export default Tree

  export{
    Node
  }

  

class Node{
  constructor(val){
    this.val=val
    this.left=this.right=undefined
  }
}

class Tree{
  constructor(data){
    let root=new Node(data.shift())
    //遍历所有的数据,逐渐插入到当前这颗搜索树中
    data.array.forEach(item => {
      this.insert(root,item)
    });
    return root
  }
  insert(node,data){
    if(node.val>data){
      if(node.left===undefined){//判断是不是左节点
        node.left=new Node(data)
      }else{
        this.insert(node.left,data)
      }
    }else{
      if(node.right===undefined){
        node.right=new Node(data)
      }else{
        this.insert(node.right,data)
      }
    }
  }

  static walk(root){
    if(!root.left&&!root.right){
      return true
    }else if((root.left&&root.val<root.left.val)||
    root.right&&root.val>root.right.val){
      return false
    }else{
      return Tree.walk(root.left)&&Tree.walk(root.right)
    }
  }
}

export default Tree
export{
  Node
}

  

原文地址:https://www.cnblogs.com/sunliyuan/p/13526182.html