Programmer interview questions 100 (60)-determine whether a binary tree is balanced

Source: Internet
Author: User

Http://zhedahht.blog.163.com/blog/static/25411174201142733927831/

Question:Enter the root node of a binary tree to determine whether the tree is balanced. If the depth difference between left and right Subtrees of any node in a binary tree does not exceed1It is a balanced binary tree.For example, the binary tree in is a balanced binary tree:

In this series of blogs, we have discussed how to find the depth of Binary Trees. With the experience of finding a binary tree, we can easily solve this problem: when traversing every node of the tree, call the treedepth function to obtain the depth of its left and right subtree. If the depth difference between left and right Subtrees of each node does not exceed 1, it is a balanced binary tree by definition. The code corresponding to this idea is as follows:

Bool isbalanced (binarytreenode * proot)

{

If (proot = NULL)

Return true;

 

Int left = treedepth (proot-> m_pleft );

Int right = treedepth (proot-> m_pright );

Int diff = left-right;

If (diff> 1 | diff <-1)

Return false;

 

Return isbalanced (proot-> m_pleft) & isbalanced (proot-> m_pright );

}

The above code is concise, but we should also note that the time efficiency of this idea is not high because a node will be traversed multiple times. For example, in the binary tree input in the isbalance function, first determine whether the left and right subtree of the root node (node with a value of 1) is a balanced node. In this case, we will input the left child root node (the node with the value of 2) to the treedepth function. We need to traverse nodes 4, 5, and 7. Then, when determining whether the subtree with a node of 2 as the root node is a balance tree, it will still traverse nodes 4, 5, and 7. Undoubtedly, repeated traversal of the same node will affect the performance. Next we will look for algorithms that do not require repeated traversal.

If we use the post-sequential Traversal method to traverse every node of a binary tree, we have traversed its left and right Subtrees before traversing to a node. As long as you record the depth of each node when traversing it (the depth of a node is equal to the length of its path to the leaf node ), we can traverse and judge whether each node is balanced. The following is a reference code for this idea:

Bool isbalanced (binarytreenode * proot, int * pdepth)

{

If (proot = NULL)

{

* Pdepth = 0;

Return true;

}

 

Int left, right;

If (isbalanced (proot-> m_pleft, & left)

& Isbalanced (proot-> m_pright, & right ))

{

Int diff = left-right;

If (diff <= 1 & diff> =-1)

{

* Pdepth = 1 + (left> right? Left: right );

Return true;

}

}

 

Return false;

}

We only need to input the root node of the binary tree and an integer variable that represents the depth of the node to the above function:

Bool isbalanced (binarytreenode * proot)

{

Int depth = 0;

Return isbalanced (proot, & depth );

}

In the above Code, we traverse the entire Binary Tree in descending order. After traversing the Left and Right subnodes of a node, we can determine whether it is balanced Based on the depth of its left and right subnodes and obtain the depth of the current node. When the root node of the tree is finally traversed, it determines whether the entire Binary Tree is a balanced binary tree.

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.