function Node(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')
a.left = b
a.right = c
b.left = d
b.right = e
c.left = f
function front(root){
if(!root) return
console.log(root.value)
front(root.left)
front(root.right)
}
front(a)
二叉树的中序遍历
function middle(root){
if(!root) return
middle(root.left)
console.log(root.value)
middle(root.right)
}
middle(a)
function end(root){
if(!root) return
end(root.left)
end(root.right)
console.log(root.value)
}
end(a)
function dfs(root,target){
if(!root) return false
if(root.value == target) return true
let left = dfs(root.left,target)
let right = dfs(root.right,tar)
return left || right
}
function deepClone(obj) {
let res = {};
let deep = {};
if (!obj) return;
for (let key in obj) {
if (isObject(obj[key])) {
deep[key] = deepClone(obj[key]);
} else if (isArray(obj[key])) {
res[key] = obj[key];
} else {
res[key] = obj[key];
}
}
return Object.assign(res, deep);
}
function isArray(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
}
function isObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}