Count the number of nodes at each layer of a binary tree and print the number of nodes at each layer.

Source: Internet
Author: User

Count the number of nodes at each layer of a binary tree and print the number of nodes at each layer.

First look at a Baidu interview question:

There is a binary tree that defines the height of the tree as the longest distance from the root to the leaf node. The width of the tree is the maximum value of each layer node. The area of the tree is defined as the product of height and width. Write a function to calculate the area of a binary tree. (15 points)

Reference materials for solving this problem refer to the beauty of offer and programming, respectively seeking for the binary tree height and width.


All adopt recursion:

Height reference: http://blog.csdn.net/buyingfei8888/article/details/38345591

To calculate the number of nodes on each layer, you must first obtain the height.

Code:

# Include <iostream> using namespace std; typedef struct node {int data; struct node * lchild; struct node * rchild;} Node, * pNode; void createTree (pNode & root) {int temp; scanf ("% d", & temp); if (0! = Temp) {root = (pNode) malloc (sizeof (Node); root-> data = temp; createTree (root-> lchild ); createTree (root-> rchild);} else {root = NULL;} void printAll (const pNode root) {pNode p = root; if (p) {cout <p-> data <"; printAll (p-> lchild); printAll (p-> rchild );}} // print the int printNodeAtLevel (pNode root, int level, int & count) {if (! Root | level <0) return 0; if (0 = level) {cout <root-> data <""; count ++; return 1 ;} return printNodeAtLevel (root-> lchild, level-1, count) + printNodeAtLevel (root-> rchild, level-1, count);} // similar to post-order traversal, high efficiency, no repeated access to node void btDepth (const pNode root, int & depth) {if (NULL = root) {depth = 0; return;} int left; btDepth (root-> lchild, left); int right; btDepth (root-> rchild, right); depth = left> right? Left + 1: right + 1;} int main () {pNode root = NULL; createTree (root); cout <endl; printAll (root); cout <endl; int depth = 0; btDepth (root, depth); cout <endl <depth; cout <endl; // print each layer for (int I = 0; I <depth; I ++) {int count = 0; printNodeAtLevel (root, I, count); cout <"number of nodes:" <count <endl ;} return 0 ;}

Running result:




How to count the number of nodes in each layer of a binary tree

Import java. io .*;
Import java. util .*;

Class TreeNode {// node class
Public String data; // node data
Public TreeNode lchild, rchild; // left and right children
Public TreeNode (String d) {// node Constructor
System. out. println ("node created"); // construct a flag
Data = d;
}
Public TreeNode () {// node Constructor
System. out. println ("node created ");
}
Public void visitNode () {// output node data
System. out. print (data );
}
}
Public class BiTree {
Public BiTree () {// main class Constructor
TreeNode tn = new TreeNode (); // leave the new tree root empty
TreeNode tn2 = tn;
Tn. visitNode ();
System. out. println ("start creating tree"); // indicates the start tree.
Try {// create a binary tree
CreateBiTree (tn );
} Catch (IOException e ){
E. printStackTrace ();
}
OutputTree (tn); // outputs a binary tree by Layer
}
String instr; // receives input String variables.
String over = new String (""); // end mark
BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));
Public TreeNode createBiTree (TreeNode tn1) throws IOException {// input a string from the keyboard, input it in the first order, and create a binary tree
System. out. println ("input node ");
Try {// input node data from the keyboard
Instr = br. readLine ();
} Catch (IOException e) {// catch an exception and output the exception information
E. printStackTrace ();
}
If (instr. equals (over) {// if it is null, no node exists.
System. out. println ("null node ");
Tn1 = null;
}
Else {// otherwise, create a node and create such a cycle for the left and right children of the node
Tn1.data = instr;
Tn1.lchild = new TreeNode ();
Tn1.lchild = createBiTree (tn1.lchild );
Tn1.rchild... the remaining full text>

How to count the number of nodes in each layer of a binary tree

Import java. io .*;
Import java. util .*;

Class TreeNode {// node class
Public String data; // node data
Public TreeNode lchild, rchild; // left and right children
Public TreeNode (String d) {// node Constructor
System. out. println ("node created"); // construct a flag
Data = d;
}
Public TreeNode () {// node Constructor
System. out. println ("node created ");
}
Public void visitNode () {// output node data
System. out. print (data );
}
}
Public class BiTree {
Public BiTree () {// main class Constructor
TreeNode tn = new TreeNode (); // leave the new tree root empty
TreeNode tn2 = tn;
Tn. visitNode ();
System. out. println ("start creating tree"); // indicates the start tree.
Try {// create a binary tree
CreateBiTree (tn );
} Catch (IOException e ){
E. printStackTrace ();
}
OutputTree (tn); // outputs a binary tree by Layer

}
String instr; // receives input String variables.
String over = new String (""); // end mark
BufferedReader br = new BufferedReader (new InputStreamReader (System. in ));
Public TreeNode createBiTree (TreeNode tn1) throws IOException {// input a string from the keyboard, input it in the first order, and create a binary tree
System. out. println ("input node ");
Try {// input node data from the keyboard
Instr = br. readLine ();
} Catch (IOException e) {// catch an exception and output the exception information
E. printStackTrace ();
}
If (instr. equals (over) {// if it is null, no node exists.
System. out. println ("null node ");
Tn1 = null;
}
Else {// otherwise, create a node and create such a cycle for the left and right children of the node
Tn1.data = instr;
Tn1.lchild = new TreeNode ();
Tn1.lch... the remaining full text>

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.