Find the diameter of a binary tree

前端 未结 7 1006
旧时难觅i
旧时难觅i 2021-01-01 03:01

I am trying to find the diameter of a binary tree (Path length between any two nodes in the tree containing maximum number of nodes.) in java.

my code snippet:

相关标签:
7条回答
  • 2021-01-01 03:36

    Diameter of a Binary Tree in O(n), It will track the diameter passing through root node or not and uses the same height function to track the diameter.

    DiameterOfTree.class

    import BinaryTree.BinaryTreeNode;
    
    public class DiameterOfBinaryTree {
        private int DIAMETER = 0;
    
        public void getDiameterOfBinaryTree(BinaryTreeNode node) {
            getHeightUtil(node, DIAMETER);
            System.out.print("\n\n Maximum Diameter of the tree is : " + DIAMETER);
        }
    
        private int getHeightUtil(BinaryTreeNode node, Integer maXValue) {
            if (node == null) {
                return 0;
            }
    
            // Here we get the maximum value returned + 1 for each subtree left or 
            //  right 
            int leftHeight = getHeightUtil(node.getLeft(), maXValue); 
            int rightHeight = getHeightUtil(node.getRight(), maXValue);
    
            //finding the new diameter at a particular node and adding 1 to 
            //include that particular node as well: leftHeight + rightHeight + 1 
            DIAMETER = Math.max(DIAMETER, leftHeight + rightHeight + 1);
    
            return 1 + Math.max(leftHeight, rightHeight);
        }
    }
    

    Main.java

    package BinaryTree;
    
    public class Main {
    
        public static void main(String[] args) {
            //Initialise root
            BinaryTreeNode root = new BinaryTreeNode(40);
    
            //Create a binary Tree
            InitialiseBinaryTree initialiseBinaryTree = new InitialiseBinaryTree();
            initialiseBinaryTree.initialise(root);
    
            // Find the diameter of the binary tree 
            new DiameterOfBinaryTree().getDiameterOfBinaryTree(root);
        }
    
    }
    

    InitialiseBinaryTree.java

    package BinaryTree;
    
    class InitialiseBinaryTree {
        void initialise(BinaryTreeNode root) {
            BinaryTreeOperation bto = new BinaryTreeOperation();
            int[] data = {20, 50, 10, 30, 5,8, 25, 32, 33};
            for (int aData : data) {
                bto.insertElementInBinaryTree(root, aData);
            }
        }
    }
    

    BinaryTreeOperation.java

    package BinaryTree;
    
    class BinaryTreeOperation {
        private boolean findInBinaryTree(BinaryTreeNode node, int data) {
            return node != null &&
                    (data == node.getData() || (findInBinaryTree(node.getLeft(), data) || findInBinaryTree(node.getRight(), data)));
        }
    
        void insertElementInBinaryTree(BinaryTreeNode node, int data) {
            if (node == null) {
                new BinaryTreeNode(data);
            } else {
                insertHelper(node, data);
            }
        }
    
        private void insertHelper(BinaryTreeNode node, int data) {
            if (node.getData() > data) {
                if (node.getLeft() == null) {
                    node.setLeft(new BinaryTreeNode(data));
                } else {
                    insertHelper(node.getLeft(), data);
                }
            } else {
                if (node.getRight() == null) {
                    node.setRight(new BinaryTreeNode(data));
                } else {
                    insertHelper(node.getRight(), data);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题