Description

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 tree

1

/ \

2 3

/ \

4 5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

my program

思路:int depth(TreeNode* root) 求树的高度,int depthDiff(TreeNode* root)求root的左子树和右子树的高度和。递归求树的diameter

class Solution {
public:
int depth(TreeNode* root)
{
if (root == NULL) return 0;
return max(depth(root->left),depth(root->right))+1;
} int depthDiff(TreeNode* root)
{
if(root == NULL) return 0;
return depth(root->left)+depth(root->right);
} int diameterOfBinaryTree(TreeNode* root) {
if(root == NULL) return 0;
return max(depthDiff(root),max(diameterOfBinaryTree(root->left),diameterOfBinaryTree(root->right)));
}
};

Submission Details

106 / 106 test cases passed.

Status: Accepted

Runtime: 19 ms

Your runtime beats 18.82 % of cpp submissions.

other methods

C++ Solution with DFS

class Solution {
public:
int maxdiadepth = 0; int dfs(TreeNode* root){
if(root == NULL) return 0; int leftdepth = dfs(root->left);
int rightdepth = dfs(root->right); if(leftdepth + rightdepth > maxdiadepth) maxdiadepth = leftdepth + rightdepth;
return max(leftdepth +1, rightdepth + 1);
} int diameterOfBinaryTree(TreeNode* root) {
dfs(root); return maxdiadepth;
}
};

C++_Recursive_with brief explanation

class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
if(root == nullptr) return 0;
int res = depth(root->left) + depth(root->right);
return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));
} int depth(TreeNode* root){
if(root == nullptr) return 0;
return 1 + max(depth(root->left), depth(root->right));
}
};

LeetCode543. Diameter of Binary Tree的更多相关文章

  1. Leetcode543.Diameter of Binary Tree二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2    3 / \ 4  5 返回 3, 它 ...

  2. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  3. LeetCode——Diameter of Binary Tree

    LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...

  4. 【leetcode_easy】543. Diameter of Binary Tree

    problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...

  5. [Swift]LeetCode543. 二叉树的直径 | Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  6. 543. Diameter of Binary Tree

    https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...

  7. 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 b ...

  8. [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 b ...

  9. [LeetCode&Python] Problem 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 b ...

随机推荐

  1. winform treeview 绑定文件夹和文件

    转载:http://www.cnblogs.com/zhbsh/archive/2011/05/26/2057733.html #region treeview 绑定文件夹和文件 /// <su ...

  2. menuStrip鼠标经过自动显示菜单

    //--------------------------------------------------------------------------------- private void For ...

  3. Java汉字md5值不一致问题

    原文:http://blog.csdn.net/earthhour/article/details/51188437 通过main方法测试得到一个加密值,通过servlet request调用得到一个 ...

  4. 【java】获取解析资源文件的方法

    关于资源文件的读取,有很多种方法,下面补充了多种方法 1.java.util.ResourceBundle 使用java自带的util包下的ResourceBundle类获取,使用方法最简单 //获取 ...

  5. 重设Windows 7密码 z

    Restart the computer to boot using the CD. Once the GUI loads, press SHIFT+F10 to bring up the comma ...

  6. 定期访问WebLogic Server返回状态的脚本

    在运维过程中,经常要获悉WebLogic Server的状态以便于主动的维护,本文通过weblogic WLST脚本初步设计了一下 脚本大概为2个,一是WLST的py脚本,getStates.py c ...

  7. 【ztree系列——图标的修改】Bootstrap风格的ztree

    前段时间项目中需要用树形结构,在选取的时候参考了很多插件,经过很多尝试,最后又回归到了ztree上.以前用的界面框架是EasyUI,但是它的树结构在实现起来有点复杂,并且功能不是特别完善.dtree在 ...

  8. Private strand flush not complete错误

    Private strand flush not complete错误 (2013-10-22 11:04:58) 转载▼   分类: FAQ 最近的一个报表系统数据库,总是出现checkpoint ...

  9. 数据写入到TXT文档中

    public class FileWrite { public File file; public FileOutputStream stream = null; //每次写入都会覆盖之前的内容 pu ...

  10. nginx监听相同端口,根据域名请求不同的server

    nginx监听相同端口,根据域名请求不同的server 学习了:https://blog.csdn.net/liaosiqian/article/details/54861270 注意其中用的是rew ...