[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)
    {
        //两个量分别是节点深度,节点最大diameter
        if (root==null) return new int[]{0,0};
        int cur = 0;
        int[] l = helper(root.left);
        int[] r = helper(root.right);
        cur = Math.max(Math.max(l[1],r[1]),l[0]+r[0]+1);
        res = Math.max(res,cur);
        return new int[]{Math.max(l[0],r[0])+1,cur};
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8395000.html

时间: 2024-11-08 16:14:49

[leetcode]543. 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 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

【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] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

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(两节点的最长路径)

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

[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] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level