首页 > 其他 > 详细

543. Diameter of Binary Tree

时间:2018-05-29 23:59:19      阅读:300      评论:0      收藏:0      [点我收藏+]
/**
 * 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

原文:https://www.cnblogs.com/JTechRoad/p/9108572.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!