何为深度搜索

如上图 假如进入深度搜索
搜索顺序为
A — C — F — G —B — D — E
也就时将左子树搜索完成后没有发现目标值再去搜索右子树
代码实现二叉树的深度优先搜索
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;c.left = f;b.left = d;a.right = b;b.right = e;c.right = g;/*** 二叉树深度优先搜索* @param {*} root 开始值//根节点* @param {*} target 目标值* @returns*/function f1(root , target){if(root == null || target == null ) return false;if(root.value == target){return true;}var left = f1(root.left,target);var right = f1(root.right,target);return left || right;}console.log(f1(a,"o"))
