[經典面試題]二叉樹寬度,二叉樹寬度

來源:互聯網
上載者:User

[經典面試題]二叉樹寬度,二叉樹寬度

(1)二叉樹最大寬度

    /*---------------------------------------------    *   日期:2015-03-07    *   作者:SJF0115    *   題目: 二叉樹的最大寬度    *   來源:經典面試題    *   部落格:    -----------------------------------------------*/    #include <iostream>    #include <queue>    using namespace std;    struct TreeNode{        int val;        TreeNode *left;        TreeNode *right;        TreeNode(int x):val(x),left(NULL),right(NULL){}    };    // 二叉樹的最大寬度    int MaxWidthBinaryTree(TreeNode *root){        if(root == NULL){            return 0;        }//if        // 當前層        queue<TreeNode*> curLevel;        // 下一層        queue<TreeNode*> nextLevel;        // 最大寬度        int max = 0;        // 當前層寬度        int width = 0;        // 排入佇列        curLevel.push(root);        TreeNode *node;        while(!curLevel.empty()){            width = 0;            while(!curLevel.empty()){                ++width;                if(width > max){                    max = width;                }//if                node = curLevel.front();                curLevel.pop();                // 左子樹                if(node->left != NULL){                    nextLevel.push(node->left);                }//if                // 右子樹                if(node->right != NULL){                    nextLevel.push(node->right);                }//if            }//while            swap(curLevel,nextLevel);        }//while        return max;    }    //按先序序列建立二叉樹    int CreateBTree(TreeNode*& T){        int data;        //按先序次序輸入二叉樹中結點的值,-1表示空樹        cin>>data;        if(data == -1){            T = NULL;        }        else{            T = new TreeNode(data);            //構造左子樹            CreateBTree(T->left);            //構造右子樹            CreateBTree(T->right);        }        return 0;    }    int main() {        TreeNode *root = NULL;        CreateBTree(root);        int result = MaxWidthBinaryTree(root);        cout<<result<<endl;        return 0;    }

(2)二叉樹各層寬度

    /*---------------------------------------------    *   日期:2015-03-07    *   作者:SJF0115    *   題目: 二叉樹的最大寬度    *   來源:經典面試題    *   部落格:    -----------------------------------------------*/    #include <iostream>    #include <queue>    #include <vector>    using namespace std;    struct TreeNode{        int val;        TreeNode *left;        TreeNode *right;        TreeNode(int x):val(x),left(NULL),right(NULL){}    };    // 二叉樹寬度    vector<int> WidthBinaryTree(TreeNode *root){        vector<int> level;        if(root == NULL){            return level;        }//if        // 當前層        queue<TreeNode*> curLevel;        // 下一層        queue<TreeNode*> nextLevel;        // 當前層寬度        int width = 0;        // 排入佇列        curLevel.push(root);        TreeNode *node;        while(!curLevel.empty()){            width = 0;            while(!curLevel.empty()){                ++width;                node = curLevel.front();                curLevel.pop();                // 左子樹                if(node->left != NULL){                    nextLevel.push(node->left);                }//if                // 右子樹                if(node->right != NULL){                    nextLevel.push(node->right);                }//if            }//while            level.push_back(width);            swap(curLevel,nextLevel);        }//while        return level;    }    //按先序序列建立二叉樹    int CreateBTree(TreeNode*& T){        int data;        //按先序次序輸入二叉樹中結點的值,-1表示空樹        cin>>data;        if(data == -1){            T = NULL;        }        else{            T = new TreeNode(data);            //構造左子樹            CreateBTree(T->left);            //構造右子樹            CreateBTree(T->right);        }        return 0;    }    int main() {        TreeNode *root = NULL;        CreateBTree(root);        vector<int> result = WidthBinaryTree(root);        for(int i = 0;i < result.size();++i){            cout<<"第"<<i+1<<"層->"<<result[i]<<"個節點"<<endl;        }//for        return 0;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.