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.

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.

思路分析:

??题目要求找出二叉树的直径,二叉树的直径的定义是,任意两节点之间的最长路径,这个路径不一定会经过根节点。我们的解题思路是,最长路径肯定是以某个节点为中转,分别从这个节点的左右子节点延伸到叶子节点的两条无转向的路径和。所以只需要求出所有节点作为中转节点的路径长度,取其中最大的就得到了答案。

代码:

public class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int x){
        val=x;
    }
}
public class Solution{
    public int diameterOfBinaryTree(TreeNode root){
        if(root==null)
            return 0;
        int res=depth(root.left)+depth(root.right);
        int ledia=diameterOfBinaryTree(root.left);
        int ridia=diameterOfBinaryTree(root.right);
        return Math.max(Math.max(res,ledia),ridia);
    }
    public int depth(TreeNode root){
        if(root==null)
            return 0;
        int left=depth(root.left);
        int right=depth(root.right);
        return Math.max(left,right)+1;
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/10712402.html

时间: 2024-08-02 07:59:09

14.Diameter of Binary Tree(二叉树的直径)的相关文章

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

[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

[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

UVALive 6577 Binary Tree 二叉树的LRU串

今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一个向下无限延伸的二叉树.然后输入起点(通过只含LRU的字符串S,从根结点开始执行).LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的). 然后输入一个可选步骤串T.可以选择T中的子序,从起点开始走.然后问可以走到多少个不同的结点. 比赛的时候不会做啊╮(╯

【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 | 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

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

/** * 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)

Maximum Depth of Binary Tree 二叉树的深度

Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 此题是经典的求二叉树深度深度题目,可采用递归的方式,以此求每个节点的左子树深度和右子树深度,然后返回该节点左右子树深度最大的那个即为该节点的深度 具体代码如下: 1 /** 2 *