码迷,mamicode.com
首页 > 其他好文 > 详细

543. Diameter of Binary Tree

时间:2018-05-30 00:30:02      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:turn   tree   binary   color   efi   nbsp   pre   null   ini   

/**
 * 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

标签:turn   tree   binary   color   efi   nbsp   pre   null   ini   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!