给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :
给定二叉树
          1
         / \
        2   3
       / \     
      4   5    
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
详见:https://leetcode.com/problems/diameter-of-binary-tree/description/

C++:

方法一:

  1. class Solution {
  2. public:
  3. int diameterOfBinaryTree(TreeNode* root)
  4. {
  5. int res = 0;
  6. maxDepth(root, res);
  7. return res;
  8. }
  9. int maxDepth(TreeNode* node, int& res)
  10. {
  11. if (!node)
  12. {
  13. return 0;
  14. }
  15. int left = maxDepth(node->left, res);
  16. int right = maxDepth(node->right, res);
  17. res = max(res, left + right);
  18. return max(left, right) + 1;
  19. }
  20. };

方法二:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int diameterOfBinaryTree(TreeNode* root) {
  13. int res=0;
  14. maxDepth(root,res);
  15. return res;
  16. }
  17. int maxDepth(TreeNode* node,int &res)
  18. {
  19. if(!node)
  20. {
  21. return 0;
  22. }
  23. if(m.count(node))
  24. {
  25. return m[node];
  26. }
  27. int left=maxDepth(node->left,res);
  28. int right=maxDepth(node->right,res);
  29. res=max(res,left+right);
  30. return m[node]=(max(left,right)+1);
  31. }
  32. private:
  33. unordered_map<TreeNode*,int> m;
  34. };

参考:http://www.cnblogs.com/grandyang/p/6607318.html

543 Diameter of Binary Tree 二叉树的直径的更多相关文章

  1. [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 ...

  2. LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

    题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...

  3. [leetcode]543. Diameter of Binary Tree二叉树的直径

    题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...

  4. [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 ...

  5. 543. Diameter of Binary Tree 二叉树的最大直径

    [抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...

  6. [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 ...

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

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

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

  9. 【leetcode_easy】543. Diameter of Binary Tree

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

随机推荐

  1. cowboy中分布式节点通信

    项目开发中,web前端节点需要与远端的聊天服节点通信.聊天服使用了otp,但我对otp下的分布式通信不太清楚,造成了一些问题. 1)首先是cowboy节点的命名.具体参数是配置在工程目录rel下的vm ...

  2. spring配置数据库连接池

    1. jdbcConfig.properties文件中 jdbc.jdbcUrl=jdbc:mysql:///ssm-crudjdbc.driverClass=com.mysql.jdbc.Drive ...

  3. Persistent connections CONN_MAX_AGE django

    Persistent connections¶ Persistent connections avoid the overhead of re-establishing a connection to ...

  4. CF 888E Maximum Subsequence——折半搜索

    题目:http://codeforces.com/contest/888/problem/E 一看就是折半搜索?……然后排序双指针. 两个<m的数加起来如果>=m,一定不会更新答案.因为- ...

  5. SPOJ(后缀数组求不同子串个数)

    DISUBSTR - Distinct Substrings Given a string, we need to find the total number of its distinct subs ...

  6. ADB命令小结

    )adb devices //查看启动的所有设备 )adb kill-server //重启设备 )adb start-server //启动设备 )adb -s emulator-(通过 adb d ...

  7. SpringMVC注解说明

    @controller 通过@controller标注即可将class定义为一个controller类. @RequestMapping value 表示需要匹配的url的格式. method 表示所 ...

  8. P-Function

    题意: 对于集合 $S = {1, 2, ...., n}$ , 定义函数 $F(x) = y, x, y$ 属于 $S$,对于任何 $x$ 属于 $S$, 有 $F(F...F(x)) = x$, ...

  9. JVM 内存区域

    JVM 将内存区域划分为: Method Area(Non-Heap)(方法区) ,Heap(堆) , Program Counter Register(程序计数器) , VM Stack(虚拟机栈, ...

  10. Spring入门(四):使用Maven管理Spring项目

    让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...