[Typical interview questions] Binary Tree width and binary tree width

Source: Internet
Author: User

[Typical interview questions] Binary Tree width and binary tree width

(1) Maximum Binary Tree width

/* ------------------------------------------- * Date: * Author: SJF0115 * Subject: maximum width of binary tree * Source: Classic interview questions * blog: optional */# include <iostream> # include <queue> using namespace std; struct TreeNode {int val; TreeNode * left; TreeNode * right; TreeNode (int x ): val (x), left (NULL), right (NULL) {}}; // maximum width of the Binary Tree int MaxWidthBinaryTree (TreeNode * root) {if (root = NULL) {retu Rn 0;} // if // the queue of the current layer <TreeNode *> curLevel; // the queue of the next layer <TreeNode *> nextLevel; // The maximum width of int max = 0; // The current Layer width int width = 0; // Add to the queue 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 (); // left subtree if (node-> left! = NULL) {nextLevel. push (node-> left) ;}// if // right subtree if (node-> right! = NULL) {nextLevel. push (node-> right);} // if} // while swap (curLevel, nextLevel);} // while return max ;} // create a binary tree int CreateBTree (TreeNode * & T) {int data in sequence; // input the value of the node in the binary tree in sequence, -1 indicates the empty tree cin> data; if (data =-1) {T = NULL;} else {T = new TreeNode (data ); // construct the left subtree CreateBTree (T-> left); // construct the right subtree CreateBTree (T-> right);} return 0;} int main () {TreeNode * root = NULL; CreateBTree (root); int result = MaxWidthBinaryTree (root); cout <result <endl; return 0 ;}

(2) width of each layer of a binary tree

/* ------------------------------------------- * Date: * Author: SJF0115 * Subject: maximum width of binary tree * Source: Classic interview questions * blog: ------------------------------------------- */# 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) {}}; // Binary Tree width vector <int> WidthBinaryTree (TreeNode * root) {ve Ctor <int> level; if (root = NULL) {return level;} // if // the queue of the current layer <TreeNode *> curLevel; // The next layer of the queue <TreeNode *> nextLevel; // The current Layer width int width = 0; // Add to the queue curLevel. push (root); TreeNode * node; while (! CurLevel. empty () {width = 0; while (! CurLevel. empty () {++ width; node = curLevel. front (); curLevel. pop (); // left subtree if (node-> left! = NULL) {nextLevel. push (node-> left) ;}// if // right subtree if (node-> right! = NULL) {nextLevel. push (node-> right);} // if} // while level. push_back (width); swap (curLevel, nextLevel);} // while return level;} // create a binary tree int CreateBTree (TreeNode * & T) {int data in sequence; // enter the value of the node in the binary tree in the first order.-1 indicates the empty tree cin> data; if (data =-1) {T = NULL ;} else {T = new TreeNode (data); // construct the left subtree CreateBTree (T-> left); // construct the right subtree 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 <"no." <I + 1 <"layer->" <result [I] <"nodes" <endl ;} // for return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.