LeetCode 543. Diameter of Binary Tree
Solution1:我的答案
思路和方法就是同124题的

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int diameterOfBinaryTree(TreeNode* root) {if (!root) return 0;int ans = 0;my_longpath(root, ans);return ans;}int my_longpath(TreeNode* root, int& ans) {if (!root) return 0;int l =  my_longpath(root->left, ans);int r = my_longpath(root->right, ans);ans = max(ans, l + r);return 1 + max(l, r);}
};

【同124】LeetCode 543. Diameter of Binary Tree相关推荐

  1. Leetcode 1522. Diameter of N-Ary Tree [Python]

    对于普通二叉树的diameter. 543. Diameter of Binary Tree 543. https://leetcode.com/problems/diameter-of-binary ...

  2. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  3. LeetCode 545. Boundary of Binary Tree 二叉树边界

    LeetCode 545. Boundary of Binary Tree 二叉树边界 Given a binary tree, return the values of its boundary i ...

  4. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  5. leetcode 545. Boundary of Binary Tree

    leetcode 545. Boundary of Binary Tree 这一题考察的是树型的先序遍历,三角形正好是先序遍历的访问点,需要做的工作就是识别出是否为边界,在下面的代码中全靠flag变量 ...

  6. Leetcode: Maximum Depth of Binary Tree

    题目:算出二叉树的最大深度 解决方案:(1)BFS (2)DFS (1)BFS 一层一层往下搜索,一直找到最深的点,这里由于节点的val是没有用的,所以可以用来存储当前节点的深度,然后注意指针一定要初 ...

  7. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  8. LeetCode - Medium - 114. Flatten Binary Tree to Linked List

    Topic Tree Depth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linke ...

  9. LeetCode 1522. Diameter of N-Ary Tree(递归)

    文章目录 1. 题目 2. 解题 1. 题目 Given a root of an N-ary tree, you need to compute the length of the diameter ...

最新文章

  1. 安全报告显示2015年DDoS攻击强度创下新纪录
  2. ASP.NET MVC 异常Exception拦截
  3. Android - 广播机制和Service
  4. dedecms mysql 支持_安装dedecms MySQL 支持 不支持无法使用本系统 GD 支持Off解决办法...
  5. Qt之QThread用法
  6. 什么是jQuery?
  7. 解决ubuntu系统udev多网卡名称变化的问题
  8. 3D数学之快速傅立叶变换(Fast Fourier Transform-FFT)
  9. springboot项目识别不了pom.xml文件_Spring Boot Web 项目教程,Spring Boot的环境配置
  10. EDA技术实用教程 | 复习一 | IP核的概念和分类
  11. ae导出html,AE导出json格式的Web动画工具 Bodymovin v5.6.1 + 使用教程
  12. php 改变图片大小,php修改图片大小的方法
  13. 如何从excel中删除奇数行或偶数行
  14. 宽字符处理函数函数与普通函数对照表
  15. 音乐外链php修复版源码
  16. 高等数学 - 球坐标计算三重积分
  17. vue 子组件调用($emit)父组件方法后父组件方法如何回调子组件方法
  18. hdu5594 ZYB's Prime
  19. 如何在计算机里显示wps云文档,wps云文档怎么打开
  20. 自己如何快速制作蓝色背景证件照

热门文章

  1. Deep3DBox论文解读
  2. 二叉排序树(概念,查找,插入,删除)
  3. 测试原理_耐压测试仪绝缘电阻测试仪基本原理与选用
  4. OpenCV图像处理(14)—— 图像转视频
  5. 安装i3_330mm长显卡,240水冷,我的第一款小钢炮,雷匠R-i3装机体验
  6. python闭包技巧_案例详析:Python闭包与nonlocal关键字
  7. 用计算机考试有老师引导,河北省教师计算机考试评价题及答案
  8. 三角形一点到三边距离最小_三角形内有没有一个点到三边距离之和最小 -
  9. sql查询百分之20到百分之40的数据_FOCUS数据管理之ETL监控
  10. 零基础python数据分析自学_零基础的人,怎么自学数据分析?