Special judge binary tree (balanced binary tree, binary search, complete binary tree)

Balanced binary tree

Determine whether the balanced binary tree, you need to pay attention to the following three points:

  • Left sub tree a balanced binary tree
  • Right sub-tree a balanced binary tree
  • Height difference of about 1 or less subtree
bool isBalanced(TreeNode* root) {
    if(!root)
        return true;
    if(abs(countFloor(root->left)-countFloor(root->right))>1) //左右子树高度差
        return false;
    else
    {
        if(isBalanced(root->left) && isBalanced(root->right)) //左右子树是否都为平衡二叉树
        {
            return true;
        }
        else
            return false;
    }
}

int countFloor(TreeNode* root) //计算树的深度
{
    if(!root)
        return 0;
    return 1+max(countFloor(root->left),countFloor(root->right));
}

Binary tree search

The binary tree binary tree traversal sequence, the result is complete binary tree search is ascending.

bool isVaildBST(TreeNode* root) 
{
    stack<TreeNode*> tmp;
    int min = INT_MIN;
    while(root != NULL || !tmp.empty())
    {
        if(root != NULL) 
        {
            tmp.push(root);
            root = root->left;
        }
        else
        {
            TreeNode* cur = tmp.top(); 
            tmp.pop();
            if(cur->val <= min)
                return false;
            min = cur->val;
            root = cur->right;
        }    
    }
    return true;          
}

Full Nimata 树

In the binary tree, except the bottom node may not be filled, the remaining maximum number of nodes each, and the bottom layer of nodes are concentrated in a plurality of layers of the leftmost position. If the bottom of the h layer, the layer comprises 1 ~ 2 ^ h nodes.

Can be judged by the level traversing, the following situation is definitely not complete binary tree.

  • Right node only son left no sons.
  • There have been a node is a leaf node, there is a node in the node after traversing the pair is not a leaf node.
bool isVaildCBT(TreeNode* root) 
{
    queue<TreeNode*> q1;
    bool leaf = false;
    if(root == NULL)
        return true;
    q1.push(root);
    while(!q1.empty())
    {
        TreeNode* cur = q1.front();
        q1.pop();
        if(cur->left == NULL && cur->right != NULL) //存在右儿子但是没有左儿子
            return false;
        if(leaf && (cur->left != NULL || cur->right != NULL)) //在出现叶节点之后存在非叶节点
            return false;
        if(cur->left != NULL)
            q1.push(cur->left);
        if(cur->right != NULL)
            q1.push(cur->right);
        else
            leaf = true;
    }
    return true;   
}

Seeking the number of nodes complete binary tree

The easiest way is to traverse the entire binary tree, but this time complexity is very high. If the requirement is less than the time complexity O (N), the characteristics can be used to simplify the process complete binary tree. Finally one complete binary tree, the node must be arranged consecutively from left to right. There must be a complete binary tree of subtrees full, a full binary tree and the number of nodes may be obtained directly from the tree height is 2 ^ h-1. Therefore, by determining the sub-tree as a complete binary tree in full binary tree, it can be less than O (N) time obtained number of nodes.

          O      
       /     \
      O       O
    /  \     /  \
   O     O  O    O
 /  \   /  \
O    O O    O
在一个节点的左子树的左路到达最底层,而右节点的左路没有到达底层,说明右子树为满二叉树,根据右子树的树高k可以得到节点数为2^k-1,加上根节点共有2^k个节点。
          O      
       /     \
      O       O
    /  \     /  \
   O     O  O    O
          /  \   /  \
         O    O O    O
如果左子树的左路没有到达最底层,而右节点的左路到底底层,与上同理。
int countNodes(TreeNode* root)
{
    if (root == NULL)
        return 0;
    return countBS(root, 1, mostleftlevel(root, 1));
}

int countBS(TreeNode* head, int level, int depth)
{
    if (level == depth) //当层数与深度相等,仅有一个节点
        return 1;
    if (mostleftlevel(head->right, level + 1) == depth) //左子树为满树
        return (1 << (depth - level)) + countBS(head->right, level + 1, depth);
    else //右子树为满树
        return (1 << (depth - level - 1)) + countBS(head->left, level + 1, depth);
}

int mostleftlevel(TreeNode* node, int level) //计算一个节点的左路深度
{
    while (node != NULL)
    {
        level++;
        node = node->left;
    }
    return level - 1;
}

Guess you like

Origin www.cnblogs.com/gcheeze/p/11084049.html