如何找到只有左 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/