Skip to main content

Check if all leaf nodes are at same level in vb.net

Vb program for Check if all leaf nodes are at same level. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net program for
'  Check if all leaves are at same level 

'  Binary Tree Node
Public Class TreeNode 
    Public  data As Integer
    Public  left As TreeNode
    Public  right As TreeNode
    
    Public Sub New(ByVal data As Integer)
        '  Set node value
        Me.data = data
        Me.left = Nothing
        Me.right = Nothing
    End Sub

End Class
public Class BinaryTree 
    Public  root As TreeNode
    Public  depth As Integer
    
    Public Sub New()
        
        '  Set initial tree root
        Me.root = Nothing
        Me.depth = -1
    
    End Sub
    
    Public Sub checkLeafHeight(ByVal node As TreeNode, 
                               ByVal level As Integer)
        if (node IsNot Nothing) Then 
            '  Test node is leaf or not
            if (node.left  Is  Nothing AndAlso
		 node.right  Is  Nothing) Then 
                if (Me.depth = -1) Then 
                    '  This is first leaf node
                    '  Get node level
                    Me.depth = level
                ElseIf (Me.depth <> level) Then 
                    '  When two leaf nodes is different level
                    Me.depth = -2
                    Return
                End If

            End If

            if (Me.depth <> -2) Then 
                '  Recursively visit left and right subtree
                Me.checkLeafHeight(node.left, level + 1)
                Me.checkLeafHeight(node.right, level + 1)
            End If

        End If

    
    End Sub
    
    Public Sub isSameLevelLeaf()
        Me.depth = -1
        '  Test
        Me.checkLeafHeight(Me.root, 1)
        '  Display result
        if (Me.depth < 0) Then 
            '  Two possible case
            '  1) When tree empty
            '  2) When leaf node are not same level
            Console.WriteLine("No")
        Else 
            '  When leaf node is same level
            Console.WriteLine("Yes")
        End IF

    
    End Sub
    
    Public Shared Sub Main(ByVal args As String())
        '  Create new tree
        Dim tree As BinaryTree = New BinaryTree()
        '    Make A Binary Tree
        '    --------------------
        '      1
        '     /  \
        '    2    3
        '   /    /  \
        '  4    5    6
        ' insertion of binary Tree nodes
        tree.root = New TreeNode(1)
        tree.root.left = New TreeNode(2)
        tree.root.right = New TreeNode(3)
        tree.root.right.right = New TreeNode(6)
        tree.root.right.left = New TreeNode(5)
        tree.root.left.left = New TreeNode(4)
        tree.isSameLevelLeaf()
        '  Add new node
        tree.root.left.left.left = New TreeNode(7)
        '   When add new node
        '    -----------------------
        '          1
        '         /  \
        '        2    3
        '       /    /  \
        '      4    5    6
        '     /
        '    7  
        tree.isSameLevelLeaf()
    
    End Sub

End Class

Output

Yes
No




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment