给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。注意:两结点之间的路径长度是以它们之间边的数目表示。详见:https://leetcode.com/problems/diameter-of-binary-tree/description/C++:
方法一:
class Solution {public: int diameterOfBinaryTree(TreeNode* root) { int res = 0; maxDepth(root, res); return res; } int maxDepth(TreeNode* node, int& res) { if (!node) { return 0; } int left = maxDepth(node->left, res); int right = maxDepth(node->right, res); res = max(res, left + right); return max(left, right) + 1; }};
方法二:
/** * 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) { int res=0; maxDepth(root,res); return res; } int maxDepth(TreeNode* node,int &res) { if(!node) { return 0; } if(m.count(node)) { return m[node]; } int left=maxDepth(node->left,res); int right=maxDepth(node->right,res); res=max(res,left+right); return m[node]=(max(left,right)+1); }private: unordered_mapm;};
参考:http://www.cnblogs.com/grandyang/p/6607318.html