首页 > 其他分享 >leetcode-543-easy

leetcode-543-easy

时间:2023-01-22 20:12:36浏览次数:46  
标签:right int tree maxDiameter diameterOfBinaryTreeRec 543 easy root leetcode

Diameter of Binary Tree

Given the root of a binary tree, return 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.

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

Example 1:


Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
Example 2:

Input: root = [1,2]
Output: 1
Constraints:

The number of nodes in the tree is in the range [1, 104].
-100 <= Node.val <= 100

思路一:树的直径等于左子树+右子树,每次遍历节点是对比并存储最大直径

    public int diameterOfBinaryTree(TreeNode root) {
        maxDiameter = 0;
        diameterOfBinaryTreeRec(root);
        return maxDiameter;
    }

    private static int maxDiameter = 0;
    public int diameterOfBinaryTreeRec(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int left = diameterOfBinaryTreeRec(root.left);
        int right = diameterOfBinaryTreeRec(root.right);
        maxDiameter = Math.max(maxDiameter, left + right);
        return Math.max(left, right) + 1;
    }

标签:right,int,tree,maxDiameter,diameterOfBinaryTreeRec,543,easy,root,leetcode
From: https://www.cnblogs.com/iyiluo/p/17064606.html

相关文章

  • leetcode-530-easy
    MinimumAbsoluteDifferenceinBSTGiventherootofaBinarySearchTree(BST),returntheminimumabsolutedifferencebetweenthevaluesofanytwodifferent......
  • leetcode-405-easy
    ConvertaNumbertoHexadecimalGivenanintegernum,returnastringrepresentingitshexadecimalrepresentation.Fornegativeintegers,two’scomplementmet......
  • leetcode-501-easy
    FindModeinBinarySearchTreeGiventherootofabinarysearchtree(BST)withduplicates,returnallthemode(s)(i.e.,themostfrequentlyoccurredelemen......
  • leetocde-374-easy
    GuessNumberHigherorLowerWeareplayingtheGuessGame.Thegameisasfollows:Ipickanumberfrom1ton.YouhavetoguesswhichnumberIpicked.Eve......
  • LeetCode最长回文子串(/dp)
    原题解题目约束解法解法一#include<iostream>#include<string>#include<vector>usingnamespacestd;classSolution{public:stringlongestPa......
  • LeetCode_单周赛_329
    2544.交替数字和代码1转成字符串,逐个判断classSolution{publicintalternateDigitSum(intn){char[]s=(""+n).toCharArray();intt......
  • 【队列】LeetCode 281. 锯齿迭代器
    题目链接281.锯齿迭代器思路使用队列进行保存代码publicclassZigzagIterator{Queue<Iterator<Integer>>queue=newLinkedList<>();publicZigzagIt......
  • 【队列】LeetCode 346. 数据流中的移动平均值
    题目链接346.数据流中的移动平均值思路队列设计基础题代码classMovingAverage{privateIntegercurrentSum=0;privateQueue<Integer>queue=newLi......
  • [LeetCode] 684. Redundant Connection
    Inthisproblem,atreeisan undirectedgraph thatisconnectedandhasnocycles.Youaregivenagraphthatstartedasatreewith n nodeslabeledfrom......
  • leetcode笔记——328周赛
    1.二维前缀和,二维差分304.二维区域和检索-矩阵不可变-力扣(LeetCode)二维前缀和怎么处理,注意加减的下标 2.2537.统计好子数组的数目-力扣(LeetCode)首先看到子数......