543.二叉树的直径

这里写自定义目录标题


题目描述

题目解答

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int res = 0;
    public int depth(TreeNode node) {
        if(node == null){
            return 0;
        }
        int left = depth(node.left);
        int right = depth(node.right);
        res = Math.max(res,left+right);
        return Math.max(left,right)+1;
    }    
    public int diameterOfBinaryTree(TreeNode root) {
        depth(root);
        return res;
    }
}
相关推荐
信奥卷王15 分钟前
[GESP202609 六级] 分树规划
算法
夏幻灵22 分钟前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
徐小黑ACG2 小时前
Golang 基础05 结构体struct
开发语言·算法·golang
Navigator_Z2 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
北极有牛3 小时前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记3 小时前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
浮链序3 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室3 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
liliangcsdn4 小时前
派息率指标的应用探索和分析
大数据·算法