Calculate the maximum depth and minimum depth of a binary tree and the difference between them.

Source: Internet
Author: User
The maxheight function is used to calculate the maximum depth of the left and right sub-trees of a binary tree. The minheight function is used to calculate the minimum depth of the left and right sub-trees of a binary tree, the isbalance function is used to calculate the depth difference between the left subtree and the right subtree. if not greater than 1, a balanced binary tree is used. Balanced Binary Tree: it is an empty tree or its left and right subtree. The absolute value of the height difference cannot exceed 1, and both left and right Subtrees are a balanced binary tree. Static void main (string [] ARGs) {node root = new node (); node C1 = new node (); node C2 = new node (); root. left = C1; root. right = c2; node C11 = new node (); c1.left = C11; node C112 = new node (); c11.right = C112; program P = new program (); console. writeline (P. isbalanced (Root); console. read ();} public int getmaxheight (node root) {If (root = NULL) return 0; int max = 1 + math. max (this. getmaxheight (root. left), this. getmaxheight (root. right); Return Max;} public int getminheight (node root) {If (root = NULL) return 0; int min = 1 + math. min (this. getminheight (root. left), this. getminheight (root. right); Return min;} public bool isbalanced (node root) {int max = This. getmaxheight (Root); int min = This. getminheight (Root); If (max-min> 1) return false; else return true;} public class node {public int value; Public node left; Public node right ;}

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.