Java Program to Calculate the Difference Between the Sum of the Odd Level and the Even Level Nodes of a Binary Tree
Graph Traversal using DFS is an obvious way to traverse a tree with recursion. Below is an algorithm for traversing binary tree using DFS.
- Initialize the current node as root node and the parent as -1.
- Traverse the Binary Tree as the in the general DFS fashion and keep of increasing the level of the node as we traverse farther from the root node.
- While traversing we check if the level of the current node of the binary tree is even then add in even sum else add in odd sum.
- Finally, print the Absolute difference of the of even sum and the odd sum.
Absolute difference of sum of odd and even nodes of a binary tree 4
Time Complexity: O(V + E) where V is the vertices and E is the edges.