Title: Enter the root node of a binary tree to determine whether the tree is a balanced two-pronged tree. If the depth of the left and right subtree of any node in a binary tree is not more than 1, then it is a balanced binary tree. For example, the two-fork tree in the figure below is a balanced binary tree:
In the 27th question of this series, we have described how to find the depth of a binary tree. With the experience of finding the depth of the binary tree and then solving the problem, it is easy to think of a thought: when traversing each node of the tree, the call function treedepth the depth of its left and right subtree. If the depth of the left and right subtree of each node does not exceed 1, it is defined as a balanced two-fork tree. This idea corresponds to the following code:
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 also need to note that because a node will be repeated traversal multiple times, this idea of time efficiency is not high. For example, in function Isbalance, enter the two-fork tree in the above figure, first to determine whether the root node (the node with a value of 1) is a balanced node. At this point we will enter the Zogen node (a node with a value of 2) to the function treedepth, and we need to traverse nodes 4, 5, and 7. The next step is to determine whether a tree with a node with a value of 2 is a tree, and still traverse nodes 4, 5, and 7. There is no doubt that repeated traversal of the same node can affect performance. Next we look for algorithms that do not require repeated traversal.
If we iterate through each node of the binary tree in a subsequent traversal, we have traversed its left and right subtree before traversing to a node. As long as you log the depth of each node (the depth of a particular node is equal to the length of its path to the leaf node), we can iterate over each node to see if it is balanced. Here is the 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;
}
All we need to do is pass the above function to the root node of the binary tree and a shape variable to indicate the depth of the node:
BOOL Isbalanced (binarytreenode* proot)
{
int depth = 0;
Return isbalanced (Proot, &depth);
}
In the code above, we iterate through the entire binary tree in the form of a subsequent traversal. After traversing the left and right sub nodes of a node, we can judge whether it is balanced according to the depth of its left node and the depth of the current node. When the last traversal to the root of the tree node, it is also judged that the whole binary tree is not a balanced binary tree.
Bo Master He Haitao to this blog article enjoy copyright. The network reprint please indicate the source http://zhedahht.blog.163.com/. Please contact the author to organize your publication. Have any suggestion to solve the problem, welcome to inform in the comment, or add me micro bo Http://weibo.com/zhedahht or http://t.163.com/zhedahht discuss with me. Thank you.