1. 程式人生 > >JavaScript刷LeetCode -- 222. Count Complete Tree Nodes

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)
  }
}

  如果本文對您有幫助,歡迎關注微信公眾號,為您推送更多內容,ε=ε=ε=┏(゜ロ゜;)┛。