543. Diameter of Binary Tree (二叉树)
来源:互联网 发布:java new一个女朋友 编辑:程序博客网 时间:2024/06/02 04:13
https://leetcode.com/problems/diameter-of-binary-tree/description/
题目:这道题让我们求二叉树的直径。
思路:求出每一个节点的左右子树的直径。更新最大值。然后当前节点的长度等于左右子树节点的最大值加1。
class Solution {public: int dfs(TreeNode *root) { if(root==NULL) return 0; int l=dfs(root->left); int r=dfs(root->right); sum=sum>l+r?sum:l+r; return l>r?l+1:r+1; } int diameterOfBinaryTree(TreeNode* root) { sum=0; dfs(root); return sum; } private: int sum;};
阅读全文
0 0
- 543. Diameter of Binary Tree (二叉树)
- 543. Diameter of Binary Tree (二叉树的直径)
- 【二叉树】树的直径【543. Diameter of Binary Tree】
- 543. Diameter of Binary Tree 二叉树的直径
- 543. Diameter of Binary Tree | 二叉树的“直径”
- 543. Diameter of Binary Tree 二叉树的直径
- leetcode解题之543. Diameter of Binary Tree Java版 (二叉树的最大直径)
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 543. Diameter of Binary Tree
- 4.Dijkstra单点最短路径算法 迪杰斯特拉
- 基于VideoView的自定义控制面板的视频播放器
- SpringMVC笔记(一)——请求流程
- 完全数
- 第六节第二大特性继承
- 543. Diameter of Binary Tree (二叉树)
- Vue之class的
- PAT (Advanced) 1018. Public Bike Management (30)
- Java Switch语句概述
- Android.mk文件分析
- Linux学习——Linux常用命令-链接命令
- this 指针
- 按键控制led亮灭
- 瘦身