This program checks if a binary tree is height-balanced. A binary tree is considered height-balanced if for every node, the height difference between its left and right subtree is no more than one.

Program Explanation

The approach involves a recursive function that traverses the tree while checking the height of each subtree. The function returns the height of the subtree if it is balanced, and -1 if it finds any part of the tree that is not balanced.

Code Structure

  1. TreeNode struct: Defines the structure of the binary tree nodes.
  2. isBalanced function: Determines if the binary tree is balanced by invoking a helper function.
  3. checkHeight function: Recursive helper function that checks the height of each subtree and determines if the tree is balanced at each node.

Code with Documentation

package main

import (
    "fmt"
    "math"
)

// TreeNode defines a binary tree node.
type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

// isBalanced checks if the binary tree is height-balanced.
func isBalanced(root *TreeNode) bool {
    return checkHeight(root) != -1
}

// checkHeight returns the height of the subtree if it is balanced, otherwise -1.
func checkHeight(node *TreeNode) int {
    if node == nil {
        return 0
    }

    leftHeight := checkHeight(node.Left)
    if leftHeight == -1 {
        return -1
    }

    rightHeight := checkHeight(node.Right)
    if rightHeight == -1 {
        return -1
    }

    if math.Abs(float64(leftHeight-rightHeight)) > 1 {
        return -1
    }

    return 1 + math.Max(float64(leftHeight), float64(rightHeight))
}

func main() {
    // Example binary tree
    root := &TreeNode{1, &TreeNode{2, &TreeNode{3, &TreeNode{4, nil, nil}, nil}, nil}, &TreeNode{2, nil, &TreeNode{3, nil, &TreeNode{4, nil, nil}}}}

    if isBalanced(root) {
        fmt.Println("The tree is balanced.")
    } else {
        fmt.Println("The tree is not balanced.")
    }
}

Conclusion

The Go code efficiently checks if a binary tree is height-balanced using a depth-first traversal approach. This ensures that the function can early terminate if any part of the tree is unbalanced, thereby optimizing performance. This functionality is crucial for applications that require well-formed binary trees for quick operations, such as AVL trees used in databases.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)