[GeeksForGeeks] Diameter of a Binary Tree

The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).

The diameter of a tree T is the largest of the following.

1. the diameter of T‘s left subtree

2. the diameter of T‘s right subtree

3. the longest path between leave nodes that goes through the root of T. This can be computed from the heights of the subtrees of T.

O(n) runtime, O(h) space, h is the height of the given binary tree.

 1 class ReturnEntry {
 2     int diameter;
 3     int height;
 4     ReturnEntry(int d, int h) {
 5         diameter = d;
 6         height = h;
 7     }
 8 }
 9 public class DiameterOfBinaryTree {
10     public static int getDiameter(TreeNode root) {
11         return getDiameterRecur(root).diameter;
12     }
13     private static ReturnEntry getDiameterRecur(TreeNode node) {
14         ReturnEntry ret = new ReturnEntry(0, 0);
15         if(node == null) {
16             return ret;
17         }
18         ReturnEntry left = getDiameterRecur(node.left);
19         ReturnEntry right = getDiameterRecur(node.right);
20         ret.diameter = Math.max(left.height + right.height + 1, Math.max(left.diameter, right.diameter));
21         ret.height = Math.max(left.height, right.height) + 1;
22         return ret;
23     }
24 }

Related Problems

[LintCode] Binary Tree Longest Consecutive Sequence II

时间: 2024-10-05 08:39:42

[GeeksForGeeks] Diameter of a Binary Tree的相关文章

[geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively

543. Diameter of Binary Tree

题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a bina

LeetCode 543. Diameter of Binary Tree (二叉树的直径)

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

[LeetCode] Diameter of Binary Tree 二叉树的直径

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

leetcode543 - Diameter of Binary Tree - easy

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Given a binary

543. Diameter of Binary Tree(两节点的最长路径)

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t

14.Diameter of Binary Tree(二叉树的直径)

Level: ??Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Exam

[LC] 543. Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

Leetcode-543-Diameter of Binary Tree

543. Diameter of Binary Tree Add to List DescriptionSubmissionsSolutions Total Accepted: 2826 Total Submissions: 6539 Difficulty: Easy Contributors: nagasupreeth Given a binary tree, you need to compute the length of the diameter of the tree. The dia