Site icon

Find vertical order sum in a binary tree ( java / DFS / example)

Veritcal Order Sum in a binary tree

Examples – find vertical order sum in a binary tree using java.

Example 1: calculate vertical order sum using recursive algorithm

Fig 1: Vertical distance binary tree

The vertical order sum of binary tree (Fig 1) is as follows:

S. No.Vertical distance  Node Sum
10 A 50
2-1 B 25
31 C 75

Example 2: find vertical sum of binary tree using depth first search

Fig 2: Vertical distance at each node

Once we know the vertical distance of each nodes, we can simply add the sum of nodes at same vertical distance. The vertical order sum of binary tree (shown in Fig 2) is as follows:

S. No.Vertical distance  Node(s) Sum
10 A, E, F 50+40+6=150
2-1 B 25
3-2 D 10
41 C 75
52 G 90
We have calculated the sum of nodes at same vertical distance (refer Fig 3).
Fig 3: Vertical order sum binary tree

Code – vertical order sum of a binary tree in java (recursive / DFS)

1.) VerticalOrderSumOfBTree class:

package org.learn.Question;

import java.util.HashMap;
import java.util.Map;

public class VerticalOrderSumOfBTree {
	private static Map<Integer, Integer> map = null;

	private static void verticalOrder(Node root, int distance) {
		if (null == root)
			return;
		int existingValue = 0;
		if (map.containsKey(distance)) {
			existingValue = map.get(distance);
		}
		map.put(distance, root.data + existingValue);
		verticalOrder(root.left, distance - 1);
		verticalOrder(root.right, distance + 1);
	}

	public static void verticalOrderSumOfBTree(Node root) {
		if (null == map) {
			map = new HashMap<Integer, Integer>();
		} else {
			map.clear();
		}
		verticalOrder(root, 0);
		map.forEach((k, v) -> System.out.println("Nodes at distance " + k + " = " + v));
	}
}

2.) Node Class:

package org.learn.Question;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}

	public Node() {
		this.left = null;
		this.right = null;
	}
	
	public static Node createNode(int number) {
		return new Node(number);
	}
}

3.) App Class:

package org.learn.Client;

import org.learn.Question.Node;
import org.learn.Question.VerticalOrderSumOfBTree;

public class App {
	public static void main(String[] args) {
		// root level 0
		Node A = Node.createNode(50);
		// Level 1
		Node B = Node.createNode(25);
		Node C = Node.createNode(75);
		// Level 2
		Node D = Node.createNode(10);
		Node E = Node.createNode(40);
		Node F = Node.createNode(60);
		Node G = Node.createNode(90);

		// connect Level 0 and 1
		A.left = B;
		A.right = C;
		// connect level 1 and level 2
		B.left = D;
		B.right = E;
		C.left = F;
		C.right = G;

		VerticalOrderSumOfBTree.verticalOrderSumOfBTree(A);
	}
}

Output – vertical order sum of a binary tree using java (recursion/dfs)

Nodes at distance 0 = 150
Nodes at distance -1 = 25
Nodes at distance -2 = 10
Nodes at distance 1 = 75
Nodes at distance 2 = 90

Download code – find vertical Order sum in binary tree

Exit mobile version