基础算法之二叉树遍历

基础算法之二叉树遍历


二叉树的遍历分为前序、中序和后序遍历
---------------有不足之出还需要各位在评论区批评指正
正文:
在这里插入图片描述
····前序遍历:是指首先从根节点开始,再依次寻找左节点、右节点。 打印顺序为: r , a , c , d ,b , f
代码为:

function before(root){
    
    
    if(root === null) return
    console.log(root.value)
    before(root.left)
    before(root.right)
    // r , a , c , d ,b , f
}
····中序遍历:是指首先从左节点开始遍历,再到根节点,然后到右节点。
	打印顺序为:c , a , d , r , d , f
  function mid(root){
    
    
  	  if(root === null) return
  	  mid(root.left)
  	  console.log(root.value)
  	  mid(root.right)
  	  //c , a , d , r , d , f
}
····后序遍历:是指首先从左节点开始遍历,依次是右节点、根节点。	
打印顺序为: c , d , a , f , b , r
  function after(root){
    
    
  	  if(root === null) return
  	  after(root.left)
  	  after(root.right)
  	   console.log(root.value)
  	  // c , d , a , f , b ,  r
}

猜你喜欢

转载自blog.csdn.net/zjk325698/article/details/108965372
今日推荐