543求二叉树的直径

543求二叉树的直径

 

 

 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxres=0;
    int diameterOfBinaryTree(TreeNode* root) {
        int high=hight(root);
        return maxres;
    }
    int hight(TreeNode* root)
    {
        if(root==nullptr)
        {
            return 0;
        }
        int left_h=hight(root->left);
        int right_h=hight(root->right);
        //应该是在递归求以每个节点作为根时树的深度时的最大值 因为不一定就是以root为根时结果会最大
        maxres=max(maxres,left_h+right_h);
        return max(left_h,right_h)+1;
    }
};

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
543求二叉树的直径 543求二叉树的直径 543求二叉树的直径 543求二叉树的直径   TRANSLATE with 543求二叉树的直径 COPY THE URL BELOW 543求二叉树的直径 543求二叉树的直径 Back EMBED THE SNIPPET BELOW IN YOUR SITE 543求二叉树的直径 Enable collaborative features and customize widget: Bing Webmaster Portal Back
上一篇:Win10-无法启动虚拟机


下一篇:设计模式--Note5--对象性能类