JavaScript刷LeetCode -- 222. Count Complete Tree Nodes

一、题目

  Given a complete binary tree, count the number of nodes.

二、题目大意

  计算完全二叉树节点的个数。

三、解题思路

  递归遍历

四、代码实现

const countNodes = root => {
  const ans = []
  dfs(root)
  return ans.length
  function dfs (root) {
    if (!root) {
      return
    }
    ans.push(root.val)
    dfs(root.left)
    dfs(root.right)
  }
}

  如果本文对您有帮助,欢迎关注微信公众号,为您推送更多内容,ε=ε=ε=┏(゜ロ゜;)┛。

猜你喜欢

转载自blog.csdn.net/dai_qingyun/article/details/85279291