How to determine if binary tree is balanced?

前端 未结 27 1473
萌比男神i
萌比男神i 2020-11-30 16:11

It\'s been a while from those school years. Got a job as IT specialist at a hospital. Trying to move to do some actual programming now. I\'m working on binary trees now, a

相关标签:
27条回答
  • 2020-11-30 17:07
    public boolean isBalanced(TreeNode root)
    {
        return (maxDepth(root) - minDepth(root) <= 1);
    }
    
    public int maxDepth(TreeNode root)
    {
        if (root == null) return 0;
    
        return 1 + max(maxDepth(root.left), maxDepth(root.right));
    }
    
    public int minDepth (TreeNode root)
    {
        if (root == null) return 0;
    
        return 1 + min(minDepth(root.left), minDepth(root.right));
    }
    
    0 讨论(0)
  • 2020-11-30 17:07

    To have a better performance specially on huge trees you can save the height in each node so it is a trade off space Vs performance:

    class Node {
        Node left;
        Node right;
        int value;
        int height;
    }
    

    Example of implementing the addition and same for deletion

    void addNode(Node root,int v)
    {    int height =0;
         while(root != null)
         {
             // Since we are adding new node so the height 
             // will increase by one in each node we will pass by
             root.height += 1;
             height++;
             else if(v > root.value){
                root = root.left();
                }
             else{
             root = root.right();
             }
    
         }
    
             height++;
             Node n = new Node(v , height);
             root = n;         
    }
    int treeMaxHeight(Node root)
    {
     return Math.Max(root.left.height,root.right.height);
    }
    
    int treeMinHeight(Node root)
    {
     return Math.Min(root.left.height,root.right.height);
    
    }
    
    Boolean isNodeBlanced(Node root)
    {
       if (treeMaxHeight(root) - treeMinHeight(root) > 2)
           return false;
    
      return true;
    }
    
    Boolean isTreeBlanced (Node root)
    {
        if(root == null || isTreeBalanced(root.left) && isTreeBalanced(root.right) && isNodeBlanced(root))
        return true;
    
      return false;
    
    }
    
    0 讨论(0)
  • 2020-11-30 17:09

    Here's what i have tried for Eric's bonus exercise. I try to unwind of my recursive loops and return as soon as I find a subtree to be not balanced.

    int heightBalanced(node *root){
        int i = 1;
        heightBalancedRecursive(root, &i);
        return i; 
    } 
    
    int heightBalancedRecursive(node *root, int *i){
    
        int lb = 0, rb = 0;
    
        if(!root || ! *i)  // if node is null or a subtree is not height balanced
               return 0;  
    
        lb = heightBalancedRecursive(root -> left,i);
    
        if (!*i)         // subtree is not balanced. Skip traversing the tree anymore
            return 0;
    
        rb = heightBalancedRecursive(root -> right,i)
    
        if (abs(lb - rb) > 1)  // not balanced. Make i zero.
            *i = 0;
    
        return ( lb > rb ? lb +1 : rb + 1); // return the current height of the subtree
    }
    
    0 讨论(0)
提交回复
热议问题