unliar / unliar.github.io

一个已经不再使用的静态博客,新的博客在后边。
https://happysooner.com
0 stars 0 forks source link

计算二叉树节点的个数 #34

Open unliar opened 3 years ago

unliar commented 3 years ago
const countNodes = function (root) {
    if (!root) return 0;
    const queqe = [root];
    let count = 0;
    while (queqe.length > 0) {
        let p = queqe.shift();
        count++;
        if (p.left) queqe.push(p.left);
        if (p.right) queqe.push(p.right);
    }
    return count;
};