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…
题目: 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…
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 int res = 1; public int diameterOfBinaryTree(TreeNode root) { helper(root); return res-1; } public int[] helper(TreeNode root) { //两个量分别是节点深度,节点最大dia…
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…
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2   3       / \           4   5    返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3].注意:两结点之间的路径长度是以它们之间边的数目表示.详见:https://leetcode.com/problems/diameter-of-binary-t…
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…
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…
[抄题]: 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 bi…
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2    3 / \ 4  5 返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]. 注意:两结点之间的路径长度是以它们之间边的数目表示. class Solution { public: int res = 0; int diameterOfBinaryTree(TreeNode* root) { if(root ==…
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeNode *root) { dfs(root); return res; } int dfs(TreeNode *root) { if (root == NULL) { return 0; } int leftNum = dfs(root->left); int rightNum = dfs(root…