二叉树节点定义如下:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
function dfs(root) {
if (root === null) {
return;
}
dfs(root.left);
dfs(root.right);
}
function bfs(root) {
let queue = new Array();
queue.push(root);
while (queue.length > 0) {
let node = queue.pop();
if (node.left) {
queue.unshift(node.left);
}
if (node.right) {
queue.unshift(node.right);
}
}
}