algorithm - 只有左 child 的二叉树中的节点数

标签 algorithm

如何找到只有左 child 的二叉树中的节点数?

LeftNode(root)
{
    if(root==NULL) 
        return 0;

    if(root->left!=null && root-right==null)
        return (1+LeftNode(root->left));

    return (LeftNode (root->left) + LeftNode (root->right))
}

最佳答案

我会这样做(C++):

int leftNode(Node * root)
{
  if (root == nullptr)
    return 0;

  // c is 1 if root has a left child and has not a right one
  int c = root->left != nullptr and root->right == nullptr ? 1 : 0;

  return c + leftNode(root->left) + leftNode(root->right);
}

关于algorithm - 只有左 child 的二叉树中的节点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34481784/

相关文章:

Python:通过附加现有列表中的信息来创建新元组

python - 给定一个数字,找到到达它的序列

java - 需要帮助理解三步动态编程/递归问题

c++ - 在容器中查找以给定字符开头的所有单词

python - 如何提高 Python 中查找最常见字符串的性能?

algorithm - 如何为水壶定义启发式函数?

algorithm - 如何在图中找到所有顶点不相交的路径?

c++顺时针排序二维点

algorithm - NP 中的语言(问题)和 P 中的语言(问题)之间的多项式时间减少

java - 为什么 long,而不是 int 否则限制时间超过