Count complete odd nodes path of binary tree in vb.net

Vb program for Count complete odd nodes path of binary tree. Here more solutions.
' Include namespace system
Imports System
' Vb.net program for
' Count odd paths in Binary Tree
' 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 Sub New()
' Set initial tree root
Me.root = Nothing
End Sub
' Count all paths from root to leaf
' which containing all Odd nodes
Public Function countOddNodePath(
ByVal node As TreeNode) As Integer
if (node Is Nothing OrElse node.data Mod 2 = 0) Then
' When tree node is null or
' its contain even value
Return 0
Else
if (node.left Is Nothing AndAlso
node.right Is Nothing) Then
' When get leaf node And
' path contain all Odd nodes
Return 1
End If
Return Me.countOddNodePath(node.left) +
Me.countOddNodePath(node.right)
End IF
End Function
Public Shared Sub Main(ByVal args As String())
' New of binary tree
Dim tree As BinaryTree = New BinaryTree()
' Construct Binary Tree
' -----------------------
' 5
' / \
' / \
' 3 9
' / \ / \
' 7 6 1 3
' / \ / \
' 10 3 7 4
' Add tree node
tree.root = New TreeNode(5)
tree.root.left = New TreeNode(3)
tree.root.right = New TreeNode(9)
tree.root.right.right = New TreeNode(3)
tree.root.left.right = New TreeNode(6)
tree.root.right.left = New TreeNode(1)
tree.root.left.left = New TreeNode(7)
tree.root.left.left.left = New TreeNode(10)
tree.root.left.left.right = New TreeNode(3)
tree.root.right.left.right = New TreeNode(4)
tree.root.right.left.left = New TreeNode(7)
' Given Binary Tree
' -----------------------
' 5
' / \
' / \
' 3 9
' / \ / \
' 7 6 1 3
' / \ / \
' 10 3 7 4
' Here
' Odd path from root to leaf
' -----------------------
' 5
' / \
' / \
' 3 9
' / / \
' 7 1 3
' \ /
' 3 7
' ---------------------
' 5->3->7->3
' 5->9->1->7
' 5->9->3
' ----------------------
' Result : 3 [number of path]
' Count odd nodes paths from root
' to leaf in a binary tree.
Console.WriteLine(tree.countOddNodePath(tree.root))
End Sub
End Class
Output
3
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