Count the number of nodes at each layer of a binary tree

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h ># include <vector> using namespace STD; struct treenode {int data; treenode * left; treenode * Right ;}; // count the number of nodes at each layer. Int res; void countnumoflevel (treenode * root, int depth) {If (root = NULL | depth <0) return; If (depth = 0) {res ++; return ;} countnumoflevel (root-> left, depth-1); countnumoflevel (root-> right, depth-1);} int main () {res = 0; treenode * t = new treenode; t-> DATA = 1; treenode * T1 = new treenode; T1-> DATA = 2; treenode * t2 = new treenode; t2-> DATA = 3; t-> left = T1; t-> right = t2; treenode * T3 = new treenode; T3-> DATA = 4; treenode * t4 = new treenode; T4-> DATA = 5; T1-> left = T3; T1-> right = T4; t2-> left = NULL; t2-> right = NULL; countnumoflevel (T, 2); printf ("% d \ n", Res); Delete t; Delete T1; Delete T2; Delete T3; delete T4; System ("pause"); 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.