[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 ;}