543. Diameter of Binary Tree
Posted The Tech Road
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了543. Diameter of Binary Tree相关的知识,希望对你有一定的参考价值。
/** * 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 res = 0; int diameterOfBinaryTree(TreeNode* root) { helper(root); return res; } int helper(TreeNode* root) { if (root == NULL) return 0; int left = helper(root->left) + 1; int right = helper(root->right) + 1; res = max(res, left+right-2); return max(left, right); } };
以上是关于543. Diameter of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章