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 tree

          1
         /         2   3
       / \
      4   5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

方法一:递归

此问题在104题基础上求最短路径

原文地址:https://www.cnblogs.com/shaer/p/10563931.html

时间: 2024-11-08 16:21:19

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

【leetcode_easy】543. Diameter of Binary Tree

problem 543. Diameter of Binary Tree 参考 1. Leetcode_easy_543. Diameter of Binary Tree; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10943221.html

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

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

543 Diameter of Binary Tree 二叉树的直径

给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2   3       / \           4   5    返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3].注意:两结点之间的路径长度是以它们之间边的数目表示.详见:https://leetcode.com/problems/diameter-of-binary-t

[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二叉树的直径

题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 int res = 1; public int diameterOfBinaryTree(TreeNode root) { helper(root); return res-1; } public int[] helper(TreeNode root) { //两个量分别是节点深度,节点最大dia

LeetCode算法题-Diameter of Binary Tree(Java实现)

这是悦乐书的第257次更新,第270篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543).给定二叉树,您需要计算树的直径长度. 二叉树的直径是树中任意两个节点之间最长路径的长度. 此路径可能会也可能不会通过根节点.例: 给出一棵二叉树 1 / 2 3 / 4 5 返回3,这是路径[4,2,1,3]或[5,2,1,3]的长度. 注意:两个节点之间的路径长度由它们之间的边数表示. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

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

/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public int DiameterOfBinaryTree(TreeNode root)