前序遍历(先根次序遍历)
先打印当前的,再打印左边的,再打印右边的。
如下代码
class Node {
constructor(value) {
this.value = value;
this.left - null;
this.right = null;
}
}
let a = new Node("A")
let b = new Node("B")
let c = new Node("C")
let d = new Node("D")
let e = new Node("E")
let f = new Node("F")
let g = new Node("G")
a.left = c;
a.right = b;
c.left = f;
c.right = g;
b.left = d;
b.right = e
function fl(root){
if(root == null) return;
console.log(root.value)
fl(root.left)
fl(root.right)
}
fl(a)