Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find the diameter of a binary tree, in Java?


/*
	This implementation demonstrates how 
	to efficiently find the diameter of a
	binary tree. Note that the diameter
	of a binary tree is defined as the 
	length of its longest path.

	Let n be the number of nodes and h be 
	the height of the binary tree.

	Time complexity: O(n) 
	Space complexity: O(h)
*/

public class BinaryTreeDiameter {
	BTNode BTRoot;

	public BinaryTreeDiameter() {
		/*
		 * Create tree below:
		 * * 1
		 * / 
		 * 2 3
		 * * / 
		 * * 4 5
		 */
		BTRoot = new BTNode(1, null, null);
		BTNode rootLeft = new BTNode(2, null, null);
		BTRoot.left = rootLeft;
		BTNode rootRight = new BTNode(3, null, null);
		BTRoot.right = rootRight;
		BTNode rootRightLeft = new BTNode(4, null, null);
		BTNode rootRightRight = new BTNode(5, null, null);
		rootRight.left = rootRightLeft;
		rootRight.right = rootRightRight;
	}

	public static void main(String[] args) {
		BinaryTreeDiameter application = new BinaryTreeDiameter();
		System.out.println(application.binaryTreeDiameter(application.BTRoot)); // 3
	}

	public int binaryTreeDiameter(BTNode root) {
		int[] diameter = new int[1];

		findDiameter(root, diameter);

		return diameter[0];
	}

	private int findDiameter(BTNode node, int[] diameter) {
		if (node == null) {
			return 0;
		}

		int leftHeight = findDiameter(node.left, diameter);
		int rightHeight = findDiameter(node.right, diameter);

		diameter[0] = Math.max(diameter[0], leftHeight + rightHeight);

		return 1 + Math.max(leftHeight, rightHeight);
	}

	// Class representing a binary tree node
	// with pointers to value, left, right, and parent nodes
	private class BTNode {
		int val;
		BTNode left;
		BTNode right;

		public BTNode(int val, BTNode left, BTNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: android studio json parser 
Java :: Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8 
Java :: get sum of array and return string 
Java :: instance of 
Java :: share location in android programmatically 
Java :: méthode retourne nom classe java 
Java :: where to use findviewbyid in fragment 
Java :: java swing on close 
Java :: null java 
Java :: measure view height android 
Java :: fibonacci of 6 
Java :: stringbuilder with delimiter java 
Java :: multiple spinner android 
Java :: abstract class java constructor 
Java :: remove element from char array java 
Java :: check if short int or long java 
Java :: Java Remove Elements from HashSet 
Java :: java infinity 
Java :: input arraylist in java 
Java :: login.html 
Java :: java read fule get String 
Java :: how to use java 
Java :: swagger ui java 
Java :: how to declare a interface in java 
Java :: matrix dimensions 
Java :: java class 
Java :: for loop in java 
Java :: How do you make bedrock full screen in Minecraft? 
Java :: JAVA Declaration Statements 
Java :: connect as SYSDBA java 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =