// 例如: 5
// 3 7
// 2 4 6 8
// 第二小值 3
// 树的中序遍历是由小到大
export const arr:number[] = []
export const tree:INode = {
value:5,
left:{
value:3,
left:{
value:2,
},
right:{
value:4,
}
},
right:{
value:7,
left:{
value:6,
},
right:{
value:8,
}
}
}
export interface INode {
value:number;
left?:INode;
right?:INode;
}
export function preOrder(tree:INode|undefined) {
if(!tree) return
// console.log(tree.value)
preOrder(tree?.left)
preOrder(tree?.right)
}
export function inOrder(tree:INode|undefined) {
if(!tree) return
inOrder(tree?.left)
// console.log(tree.value)
arr.push(tree.value)
inOrder(tree?.right)
}
export function postOrder(tree:INode|undefined) {
if(!tree) return
postOrder(tree?.left)
postOrder(tree?.right)
// console.log(tree.value)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)