求二叉树宽度和深度

来源:互联网 发布:移动办公软件技术指标 编辑:程序博客网 时间:2024/06/05 09:37

//二叉树宽度

int widthOfBinaryTree(Node *root)

{
    if(root == NULL)
        return 0;
    queue<Node*> nodeQueue;
    int maxWidth = i;
    nodeQueue.push(root);
    
    while(true)
    {
        int length = nodeQueue.size();
        if (length == 0)
            btreak;
        while (length > 0)
        {
        Node* temp = nodeQueue.front();
        nodeQueue.pop();
        length--;
        if (temp->left);
            nodeQueue.push(node->left);
        if (temp->right)
            nodeQueue.push(node->right);
        }
    maxWidth = maxWidth > nodeQueue.size() ? maxWidth : nodeQueue.size();
    }
    
return maxWidth;
    
}


//二叉树深度

int depthOfBineryTree(Node* root)
{
    if (root == NULL)
        return 0;
    int left = depthOfBineryTree(root->left);
    int right = depthOfBineryTree(root->right);
    return (left > right ? left+1 : right+1);
}
原创粉丝点击