Fork me on GitHub

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

Coding Poineer

树形数据结构~~排序/遍历/构造算法

function treeNode(value){
  this.left = null
  this.right = null
  this.val = value
  return this
}
function midSort(tree,arr){
	//  中序遍历
  if(tree.left) midSort(tree.left,arr)
	arr.push(tree.val)
  if(tree.right) midSort(tree.right,arr)
  return arr
}
function preSort(tree,arr){
	// 先序遍历
  arr.push(tree.val)
  if(tree.left) preSort(tree.left,arr)
  if(tree.right) preSort(tree.right,arr)
  return arr
}
function postSort(tree,arr){
	// 后序遍历
  if(tree.left) postSort(tree.left,arr)
  if(tree.right) postSort(tree.right,arr)
  arr.push(tree.val)
  return arr
}
posted @ 2020-11-10 09:23  365/24/60  阅读(329)  评论(0编辑  收藏  举报