9月20日 16:00
实习
const render = (element, container) => {
// 创建node节点,对文本节点和非文本节点做不同处理
const node = element.type === 'TEXT_ELEMENT'
? document.createTextNode('')
: document.createElement(element)
// 递归处理children
element.props.children.map(child => render(child, node))
// 将props里的属性赋给dom节点
Object.keys(element.props)
.filter(key => key !== 'children')
.forEach(name =>
node[name] == element.props.name
)
container.appendChild(node)
}
const isSymmetricTree = (root) => {#面经##校招##阿里巴巴##前端工程师#
return root === null
? true
: judge(root.left, root.right) // 递归处理左右子树
}
const judge = (left, right): boolean => {
if (left === null && right === null) {
return true
} else if (left !== null && right !== null) {
return
left.val === right.val
? judge(left.left, right.right) && judge(left.right, right.left) // 判断镜像核心逻辑
: false
} else {
return false
}
}