Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

the diameter of a binary tree


/*
	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

Diameter of Binary Tree

class TreeNode:
    '''
    Tree Node
    '''
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def __init__(self):
        self.max = 0
    def Diameter(self, root: TreeNode) -> int:
        if root is None: return 0
        def traverse(root):
            if root is None: return 0
            left = traverse(root.left)
            right = traverse(root.right)
            if left + right > self.max:
                self.max = left+right
            return max(left, right) + 1
        traverse(root)
        return self.max

if __name__ == '__main__':
    root = TreeNode(10)
    root.left = TreeNode(11)
    root.left.left = TreeNode(2)
    root.left.right = TreeNode(31)
    root.right = TreeNode(12)

    print(Solution().Diameter(root))
Comment

get diameter of binary tree

int getDiameter(TreeNode* root)
{
	if(root == NULL)
    	return(0);
    int lheight = getHeight(root->left);
    int rheight = getHeight(root->right);
    
    int ldiameter = getDiameter(root->left);
    int rdiameter = getDiameter(root->right);
    
    return(max(lheight+rheight+1,max(ldiameter,rdiameter)));
}
Comment

PREVIOUS NEXT
Code Example
Java :: final method and abstract method 
Java :: de caracter a string en java 
Java :: java program to search item from name in class 
Java :: creating jdbc connection in java using service name oracle 
Java :: android studio SELECT * FROM table 
Java :: horizontalAlignment center jlabel 
Java :: arraylist replace 
Java :: seek bar 
Java :: object null java 
Java :: get runtime java 
Java :: how to disable coming back to activity android studio 
Java :: 2048 java code 
Java :: greatest of three in java 
Java :: android studio int ot string 
Java :: finding the ascii value of a character in java 
Java :: java indexof nth occurrence 
Java :: why are string immutsble in java 
Java :: remove duplicates from list java 
Java :: lcm of two number in java 
Java :: spring boot send api request 
Java :: list remove duplicates java 
Java :: implement queue using array in java 
Java :: encryption 
Java :: void * to int 
Java :: how to initialize a variable in java 
Java :: calculating the percentile in java 
Java :: long in java 
Java :: create fragment constructor in arrayadapter 
Java :: how to change default port in spring boot 
Java :: pvector maximum dimension 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =