{"id":14891,"date":"2023-03-24T06:03:55","date_gmt":"2023-03-24T06:03:55","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=14891"},"modified":"2023-11-30T07:18:25","modified_gmt":"2023-11-30T07:18:25","slug":"diameter-of-a-binary-tree","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/","title":{"rendered":"Diameter of a Binary Tree"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\" alt=\"\" \/><\/p>\n<p>The diameter of a binary tree is a fundamental metric used in understanding the structure and efficiency of tree-based algorithms. It represents the longest path between any two nodes in the tree, measured by the number of edges between them. Calculating the diameter of a binary tree involves traversing the tree and finding the maximum distance between two nodes. This critical measurement plays a significant role in optimizing algorithms, such as tree-based data structures, network routing, and more.<\/p>\n<h2>How to Find the Diameter of a Binary Tree?<\/h2>\n<p>Given a binary tree, find its diameter. The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree and there is no compulsion on the path, it may or may not pass through the root node.<br \/>\nLet\u2019s understand this with the help of some sample examples.<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p><strong>Input<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890415-1-01%20%2831%29.png\" alt=\"\" \/><\/p>\n<p><strong>Output<\/strong><\/p>\n<pre><code>4<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn the above example, the longest path is of length 4 and the path is 7 &#8211; 4 &#8211; 8 &#8211; 1 &#8211; 3.<\/p>\n<p><strong>Example 2<\/strong><\/p>\n<p><strong>Input<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890419-1-02%20%2820%29.png\" alt=\"\" \/><\/p>\n<p><strong>Output<\/strong><\/p>\n<pre><code>6<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nIn the above example, the longest path is of length 6 and is not passing through the root. And the path is 5 &#8211; 3 &#8211; 1 &#8211; 8 &#8211; 0 &#8211; 2 &#8211; 0.<\/p>\n<h2>Solution for Finding the Diameter of Binary Tree: Brute Force<\/h2>\n<p>In this approach, we will consider every node of the binary tree as the curving point. We can understand a curving point as the diameter path which has the maximum height. In the above-mentioned example, the curving points are 4 and 8 respectively.<br \/>\nThe diameter is nothing but the maximum of the height of the left subtree+ height of the right subtree. So in this approach, we will travel to each node and calculate the height of its left and right subtree and find the maximum. After traversing the complete binary tree we will have our diameter with us.<\/p>\n<h3>Algorithm of Finding the Diameter of Binary Tree by Brute Force<\/h3>\n<p>We have explained the algorithm of the above-mentioned approach below.<\/p>\n<ul>\n<li>Travel the whole tree recursively.<\/li>\n<li>At each node calculate the height of the left and right subtree.<\/li>\n<li>Now calculate the diameter by adding the length of both the left and right subtrees.<\/li>\n<li>Now calculate the maximum diameter.<\/li>\n<li>You can do this by passing any variable by reference and with each recursive call updating the value of that variable.<\/li>\n<\/ul>\n<h3>Dry Run of the above Approach<\/h3>\n<p>Now, we will discuss the dry run for the brute force of finding the diameter of a binary tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890422-1-03%20%2813%29.png\" alt=\"\" \/><\/p>\n<ul>\n<li>First, we will initialize the maximum diameter variable with the minimum value possible so INT_MIN.<\/li>\n<li>Now on node 5, the left height is 1, right height is 4 hence the value of diameter is 1+4=5. Currently, it is the max value so currently, so the diameter is 5.<\/li>\n<li>On node 9 the left height and right height are 0 as it is a leaf node so its diameter is 0 but it is not the maximum value we have so the diameter is currently 5.<\/li>\n<li>On node 4 the left height and right height are 3 hence the diameter is 6 so is the max value now.<\/li>\n<li>On node 7 left height is 2 right height is 0 current diameter is 2 but not max.<\/li>\n<li>For node 2 the left height is 1 and the right height is 1 so the diameter is 1 but not the max value.<\/li>\n<li>12 is a leaf node so the diameter for leaf nodes like 12,0,3 in the above example will be 0.<\/li>\n<li>Similarly, we can do this for the remaining nodes, as we have explained in the image. But they will not change the value of the max diameter as they are smaller than the current max value.<\/li>\n<\/ul>\n<p>Hence the maximum value of diameter will be 6.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_14826 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_14826 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_14826 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_14826 .wpsm_nav-tabs > li.active > a, #tab_container_14826 .wpsm_nav-tabs > li.active > a:hover, #tab_container_14826 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_14826 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_14826 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_14826 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_14826 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_14826 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_14826 .wpsm_nav-tabs > li > a:hover , #tab_container_14826 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_14826 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_14826 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_14826 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_14826 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_14826 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_14826 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_14826 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14826 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_14826 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14826 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_14826 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_14826\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_14826\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14826_1\" aria-controls=\"tabs_desc_14826_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14826_2\" aria-controls=\"tabs_desc_14826_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14826_3\" aria-controls=\"tabs_desc_14826_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_14826\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_14826_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\n\r\nstruct node {\r\n    int data;\r\n    struct node *left, *right;\r\n};\r\n\r\nstruct node* newNode(int data);\r\n\r\nint max(int a, int b) { return (a &gt; b) ? a : b; }\r\n\r\nint height(struct node* node);\r\nint diameter(struct node* tree)\r\n{\tif (tree == NULL)\r\n        return 0;\r\n\r\n    int lheight = height(tree-&gt;left);\r\n    int rheight = height(tree-&gt;right);\r\n\r\n    int ldiameter = diameter(tree-&gt;left);\r\n    int rdiameter = diameter(tree-&gt;right);\r\n\r\n\r\n    return max(lheight + rheight + 1,\r\n            max(ldiameter, rdiameter));\r\n}\r\n\r\n\r\n\r\nint height(struct node* node)\r\n{\r\n    if (node == NULL)\r\n        return 0;\r\n\r\n    return 1 + max(height(node-&gt;left), height(node-&gt;right));\r\n}\r\n\r\nstruct node* newNode(int data)\r\n{\r\n    struct node* node\r\n        = (struct node*)malloc(sizeof(struct node));\r\n    node-&gt;data = data;\r\n    node-&gt;left = NULL;\r\n    node-&gt;right = NULL;\r\n\r\n    return (node);\r\n}\r\n\r\nint main()\r\n{\r\n\r\n    struct node* root = newNode(11);\r\n    root-&gt;left = newNode(21);\r\n    root-&gt;right = newNode(31);\r\n    root-&gt;left-&gt;left = newNode(14);\r\n    root-&gt;left-&gt;right = newNode(15);\r\n\r\n    cout &lt;&lt; \"Diameter of the given binary tree is \"\r\n        &lt;&lt; diameter(root);\r\n\r\n    return 0;\r\n}\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_14826_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class Node {\r\n    int data;\r\n    Node left, right;\r\n\r\n    public Node(int item)\r\n    {\r\n        data = item;\r\n        left = right = null;\r\n    }\r\n}\r\n\r\nclass BinaryTree {\r\n    Node root;\r\n\r\n    \r\n    int diameter(Node root)\r\n    {\r\n    \r\n        if (root == null)\r\n            return 0;\r\n\r\n        int lheight = height(root.left);\r\n        int rheight = height(root.right);\r\n\r\n        int ldiameter = diameter(root.left);\r\n        int rdiameter = diameter(root.right);\r\n\r\n        return Math.max(lheight + rheight + 1,Math.max(ldiameter, rdiameter));\r\n    }\r\n\r\n    int diameter() { return diameter(root); }\r\n\r\n    \r\n    static int height(Node node)\r\n    {\r\n        if (node == null)\r\n            return 0;\r\n\r\n        \r\n        return (1\r\n                + Math.max(height(node.left),\r\n                        height(node.right)));\r\n    }\r\n\r\n    public static void main(String args[])\r\n    {\r\n        BinaryTree tree = new BinaryTree();\r\n        tree.root = new Node(11);\r\n        tree.root.left = new Node(21);\r\n        tree.root.right = new Node(31);\r\n        tree.root.left.left = new Node(14);\r\n        tree.root.left.right = new Node(15);\r\n\r\n        System.out.println(\r\n            \"The diameter of given binary tree is : \"\r\n            + tree.diameter());\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_14826_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nclass Node:\r\n\r\n    \r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.left = None\r\n        self.right = None\r\n\r\ndef height(node):\r\n\r\n\r\n    if node is None:\r\n        return 0\r\n\r\n\r\n    return 1 + max(height(node.left), height(node.right))\r\n\r\n\r\n\r\ndef diameter(root):\r\n\r\n\r\n    if root is None:\r\n        return 0\r\n\r\n    \r\n    lheight = height(root.left)\r\n    rheight = height(root.right)\r\n\r\n    \r\n    ldiameter = diameter(root.left)\r\n    rdiameter = diameter(root.right)\r\n\r\n    return max(lheight + rheight + 1, max(ldiameter, rdiameter))\r\n\r\n\r\n\r\nif __name__ == \"__main__\":\r\n\r\n\r\n\r\n    root = Node(11)\r\n    root.left = Node(21)\r\n    root.right = Node(31)\r\n    root.left.left = Node(14)\r\n    root.left.right = Node(15)\r\n\r\n\r\n    print(diameter(root))\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_14826 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_14826 a\"),jQuery(\"#tab-content_14826\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>Diameter of the given binary tree is 4<\/code><\/pre>\n<p><strong>Explanation of the above example<\/strong><br \/>\nWe have to find the diameter of the binary tree using the above approach by finding the diameter at each node and then printing the max among all of them.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nThe time complexity of the above approach will be O(N*N) where N is the number of nodes in the binary tree.<\/p>\n<p><strong>Space Complexity<\/strong><br \/>\nO(1) extra space and O(H) will be recursive stack space where H is the height of the binary tree.<\/p>\n<h2>Solution for Finding the Diameter of Binary Tree: Optimized Approach<\/h2>\n<p>In this blog section, we will discuss the optimized approach for finding the diameter of a binary tree. The approach gives us the correct answer but can you think of anything this is repeating in the above approach? The answer is the height of subtrees.<br \/>\nTo solve this we can use the post-order traversal as in post-order traversal we will travel to left and right subtrees first before going to the node so that in one recursion we can find the height and diameter of the node together.<\/p>\n<h3>Algorithm of Finding the Diameter of Binary Tree by Optimized Approach<\/h3>\n<p>We will now discuss the algorithm for the above-mentioned approach.<\/p>\n<ul>\n<li>Start traversing the tree but now in the post order.<\/li>\n<li>Do all the steps like calculating the height of the left and right subtrees and the diameter of the current node but in post order.<\/li>\n<li>Compare the diameter of the current node with the stored diameter if the current diameter is maximum then update the value of the stored diameter, otherwise move on.<\/li>\n<li>Return the height of the current node to the last or previous recursive call.<\/li>\n<\/ul>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_14827 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_14827 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_14827 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_14827 .wpsm_nav-tabs > li.active > a, #tab_container_14827 .wpsm_nav-tabs > li.active > a:hover, #tab_container_14827 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_14827 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_14827 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_14827 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_14827 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_14827 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_14827 .wpsm_nav-tabs > li > a:hover , #tab_container_14827 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_14827 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_14827 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_14827 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_14827 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_14827 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_14827 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_14827 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14827 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_14827 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_14827 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_14827 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_14827\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_14827\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14827_1\" aria-controls=\"tabs_desc_14827_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14827_2\" aria-controls=\"tabs_desc_14827_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_14827_3\" aria-controls=\"tabs_desc_14827_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_14827\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_14827_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\nstruct node {\r\n    int data;\r\n    struct node *left, *right;\r\n};\r\n\r\nstruct node* newNode(int data);\r\n\r\nint diameterOpt(struct node* root, int* height)\r\n{\r\n\r\n    int lh = 0, rh = 0;\r\n\r\n\r\n    int ldiameter = 0, rdiameter = 0;\r\n\r\n    if (root == NULL) {\r\n        *height = 0;\r\n        return 0; \r\n    }\r\n\r\n    \r\n    ldiameter = diameterOpt(root-&gt;left, &amp;lh);\r\n    rdiameter = diameterOpt(root-&gt;right, &amp;rh);\r\n\r\n    *height = max(lh, rh) + 1;\r\n\r\n    return max(lh + rh + 1, max(ldiameter, rdiameter));\r\n}\r\n\r\nstruct node* newNode(int data)\r\n{\r\n    struct node* node\r\n        = (struct node*)malloc(sizeof(struct node));\r\n    node-&gt;data = data;\r\n    node-&gt;left = NULL;\r\n    node-&gt;right = NULL;\r\n\r\n    return (node);\r\n}\r\n\r\nint main()\r\n{\r\n\r\n    struct node* root = newNode(11);\r\n    root-&gt;left = newNode(21);\r\n    root-&gt;right = newNode(31);\r\n    root-&gt;left-&gt;left = newNode(14);\r\n    root-&gt;left-&gt;right = newNode(15);\r\n\r\n    int height = 0;\r\n\r\n    \/\/ Function Call\r\n    cout &lt;&lt; \"Diameter of the given binary tree is \"\r\n        &lt;&lt; diameterOpt(root, &amp;height);\r\n\r\n    return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_14827_2\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/\/ Recursive Java program to find the diameter of a\r\n\r\nclass Node {\r\n    int data;\r\n    Node left, right;\r\n\r\n    public Node(int item)\r\n    {\r\n        data = item;\r\n        left = right = null;\r\n    }\r\n}\r\n\r\n\r\nclass Height {\r\n    int h;\r\n}\r\n\r\n\r\nclass BinaryTree {\r\n    Node root;\r\n\r\n\r\n    int diameterOpt(Node root, Height height)\r\n    {\r\n        \r\n        Height lh = new Height(), rh = new Height();\r\n\r\n        if (root == null) {\r\n            height.h = 0;\r\n            return 0; \r\n        }\r\n    \r\n        int ldiameter = diameterOpt(root.left, lh);\r\n        int rdiameter = diameterOpt(root.right, rh);\r\n\r\n        \r\n        height.h = Math.max(lh.h, rh.h) + 1;\r\n\r\n        return Math.max(lh.h + rh.h + 1,\r\n                        Math.max(ldiameter, rdiameter));\r\n    }\r\n\r\n    \r\n    int diameter()\r\n    {\r\n        Height height = new Height();\r\n        return diameterOpt(root, height);\r\n    }\r\n\r\n    static int height(Node node)\r\n    {\r\n    \r\n        if (node == null)\r\n            return 0;\r\n\r\n        return (1\r\n                + Math.max(height(node.left),\r\n                        height(node.right)));\r\n    }\r\n\r\n    public static void main(String args[])\r\n    {\r\n        BinaryTree tree = new BinaryTree();\r\n        tree.root = new Node(11);\r\n        tree.root.left = new Node(21);\r\n        tree.root.right = new Node(31);\r\n        tree.root.left.left = new Node(14);\r\n        tree.root.left.right = new Node(15);\r\n\r\n        System.out.println(\r\n            \"The diameter of given binary tree is : \"\r\n            + tree.diameter());\r\n    }\r\n}\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_14827_3\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\n\r\nclass Node:\r\n\r\n    \r\n    def __init__(self, data):\r\n        self.data = data\r\n        self.left = self.right = None\r\n\r\n\r\nclass Height:\r\n    def __init(self):\r\n        self.h = 0\r\n\r\n\r\n\r\ndef diameterOpt(root, height):\r\n\r\n    lh = Height()\r\n    rh = Height()\r\n\r\n\r\n    if root is None:\r\n        height.h = 0\r\n        return 0\r\n\r\n    \r\n\r\n    ldiameter = diameterOpt(root.left, lh)\r\n    rdiameter = diameterOpt(root.right, rh)\r\n\r\n    \r\n\r\n    height.h = max(lh.h, rh.h) + 1\r\n\r\n\r\n    return max(lh.h + rh.h + 1, max(ldiameter, rdiameter))\r\n\r\n\r\n\r\n\r\ndef diameter(root):\r\n    height = Height()\r\n    return diameterOpt(root, height)\r\n\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    root = Node(11)\r\n    root.left = Node(21)\r\n    root.right = Node(31)\r\n    root.left.left = Node(14)\r\n    root.left.right = Node(15)\r\n\r\n\r\n\r\n    print(\"The diameter of the binary tree is:\", end=\" \")\r\n\r\n    print(diameter(root))\r\n\r\n\r\n<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_14827 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_14827 a\"),jQuery(\"#tab-content_14827\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>The diameter of the binary tree is: 4<\/code><\/pre>\n<p><strong>Explanation of the above approach<\/strong><br \/>\nWe have used the above-mentioned approach in the above codes as with the help of post-order traversal we can find the diameter and height in a single recursion.<\/p>\n<p><strong>Time Complexity<\/strong><br \/>\nO(N) as we are traversing the whole tree.<br \/>\n<strong>Space Complexity<\/strong><br \/>\nThe space complexity will be O(N) as we are recursing the whole tree.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nUnderstanding the diameter of a binary tree is crucial in various computer science applications. It helps in optimizing algorithms that involve tree structures, enabling efficient problem-solving across different domains. By determining the longest path between nodes within a binary tree, developers can enhance the performance and scalability of their applications. The diameter metric provides valuable insights into the structure of the tree, aiding in algorithmic optimizations and improved system design.<\/p>\n<h2>Frequently Asked Questions on Diameter of a Binary Tree<\/h2>\n<p>Here are some of the frequently asked questions about the diameter of a binary tree.<\/p>\n<p><strong>1. Can the diameter of a binary tree be negative?<\/strong><br \/>\nNo, the diameter of a binary tree cannot be negative.<\/p>\n<p><strong>2. How is the diameter of a binary tree calculated?<\/strong><br \/>\nTo calculate the diameter of a binary tree, you perform a depth-first traversal to find the longest path between two nodes. This is typically done by finding the height of the left and right subtrees for each node and then computing the maximum diameter among all nodes.<\/p>\n<p><strong>3. What is the significance of the diameter of a binary tree?<\/strong><br \/>\nThe diameter provides essential insights into the structure of the tree. It helps in optimizing algorithms that rely on tree-based data structures, such as optimizing network routing, designing efficient data storage systems, and solving various graph-related problems.<\/p>\n<p><strong>4. Can the diameter of a binary tree be zero?<\/strong><br \/>\nYes, the diameter of a binary tree can be zero. This occurs when the tree consists of either a single node or no node at all. In such cases, the maximum distance between any two nodes is zero.<\/p>\n<p><strong>5. How does the diameter impact the efficiency of algorithms?<\/strong><br \/>\nUnderstanding the diameter allows for the optimization of algorithms by providing information about the longest path within the tree. Algorithms can be designed or modified to take advantage of this information, improving their efficiency and reducing computational complexity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The diameter of a binary tree is a fundamental metric used in understanding the structure and efficiency of tree-based algorithms. It represents the longest path between any two nodes in the tree, measured by the number of edges between them. Calculating the diameter of a binary tree involves traversing the tree and finding the maximum [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[153],"tags":[],"class_list":["post-14891","post","type-post","status-publish","format-standard","hentry","category-tree"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Diameter of a Binary Tree<\/title>\n<meta name=\"description\" content=\"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Diameter of a Binary Tree\" \/>\n<meta property=\"og:description\" content=\"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-24T06:03:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-30T07:18:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Diameter of a Binary Tree\",\"datePublished\":\"2023-03-24T06:03:55+00:00\",\"dateModified\":\"2023-11-30T07:18:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\"},\"wordCount\":1268,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\",\"articleSection\":[\"Trees\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\",\"name\":\"Diameter of a Binary Tree\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\",\"datePublished\":\"2023-03-24T06:03:55+00:00\",\"dateModified\":\"2023-11-30T07:18:25+00:00\",\"description\":\"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Trees\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/tree\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Diameter of a Binary Tree\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Diameter of a Binary Tree","description":"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/","og_locale":"en_US","og_type":"article","og_title":"Diameter of a Binary Tree","og_description":"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.","og_url":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-03-24T06:03:55+00:00","article_modified_time":"2023-11-30T07:18:25+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Diameter of a Binary Tree","datePublished":"2023-03-24T06:03:55+00:00","dateModified":"2023-11-30T07:18:25+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/"},"wordCount":1268,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg","articleSection":["Trees"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/","url":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/","name":"Diameter of a Binary Tree","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg","datePublished":"2023-03-24T06:03:55+00:00","dateModified":"2023-11-30T07:18:25+00:00","description":"The diameter of a binary tree refers to the longest distance between any two nodes in the binary tree. Know the solution for finding the diameter of binary tree.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1679635890329-Diameter%20of%20a%20Binary%20Tree.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/diameter-of-a-binary-tree\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Trees","item":"https:\/\/prepbytes.com\/blog\/category\/tree\/"},{"@type":"ListItem","position":3,"name":"Diameter of a Binary Tree"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=14891"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14891\/revisions"}],"predecessor-version":[{"id":18445,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/14891\/revisions\/18445"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=14891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=14891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=14891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}