- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between sums of odd position and even position nodes of a Binary Tree in Java
Problem Statement
With a given binary tree, write a program to find the difference between sum of nodes at odd position and even position. Assume root at level 0, odd position, left/right child of root at level 2, left child at odd position and right child at even position and so on.
Example
5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9 Sum of nodes at odd positions = 5 + 2 + (1 + 8) + (3 + 9) = 5 + 2 + 9 + 12 = 28 Sum of nodes at even level = 0 + 6 + 4 + 7 = 17 Difference = 11.
Solution
Use Level Order Traversal. During traversal, mark first element as odd position and then switch to even when new element is encountered and then switch back on next and so on.
Example
Following is the program in Java to find the required output.
import java.util.LinkedList; class Node { int data; Node left, right; Node(int data){ this.data = data; this.left = this.right = null; } } public class JavaTester { public static Node getTree(){ Node root = new Node(5); root.left = new Node(2); root.right = new Node(6); root.left.left = new Node(1); root.left.right = new Node(4); root.left.right.left = new Node(3); root.right.right = new Node(8); root.right.right.right = new Node(9); root.right.right.left = new Node(7); return root; } public static int difference(LinkedList<Node> queue){ if(queue.isEmpty()) return 0; int evenSum = 0; int oddSum = 0; while(true){ int nodes = queue.size(); if(nodes == 0) break; boolean isOdd = true; while(nodes > 0){ Node node = queue.peek(); if(isOdd) oddSum += node.data; else evenSum += node.data; queue.remove(); nodes--; if(node.left != null) queue.add(node.left); if(node.right != null) queue.add(node.right); isOdd = !isOdd; } } return oddSum - evenSum; } public static void main(String args[]){ Node tree = getTree(); LinkedList<Node> queue = new LinkedList<Node>(); queue.add(tree); System.out.println(difference(queue)); } }
Output
11
- Related Articles
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Difference between sums of odd and even digits.
- Python Program for Difference between sums of odd and even digits
- C Program for Difference between sums of odd and even digits?
- C Program for the Difference between sums of odd and even digits?
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++
- How to get odd and even position characters from a string?
- Difference between Binary Tree and Binary Search Tree
- Find distance between two nodes of a Binary Tree in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Find distance between two nodes of a Binary Tree in C++ Program
- Difference Between B-tree and Binary tree
- Find sum of even and odd nodes in a linked list in C++
- Convert a tree to forest of even nodes in C++

Advertisements