算法练习-二叉树的直径

53 阅读1分钟

被标记为简单题,不是很明白为啥,直到想出题解:

最长二叉树路径的两端都必然存在一个中心节点,计算中心节点的左子树+右子树更新二叉树的最长路径。

后序遍历,对每个节点更新其左右子树的深度和是否大于最大直径

/**
 * 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 maxLength;
    int dfs(TreeNode* root) {
        if (!root) { return 0;}
        if (!root->left && !root->right) { return 1;}
        int leftDeepth  =  dfs(root->left); 
        int rightDeepth =  dfs(root->right); 
        if (maxLength < (leftDeepth + rightDeepth)) {
            maxLength = (leftDeepth + rightDeepth);
        }
        int rootDeepth = 1 + (leftDeepth > rightDeepth ? leftDeepth : rightDeepth);
        return rootDeepth;
    }

    /*大概是某个节点拥有最大深度左子树,某个节点拥有最大深度右子树,再加上这两节点的距离
    思路:
        1. 从根节点开始遍历,计算当前节点的左子树深度-右子树深度, 作为最长直径
        - 可能情况:左子树节点有两长串 => 计算左节点时,自然会统计到两长串的深度
      
        * Case List 
        1.左子树 -> Root -> 右子树
        2.小左子树 -> 左节点 -> 左节点的右子树
    */
    int diameterOfBinaryTree(TreeNode* root) {
        maxLength = 0;
        dfs(root);
        return maxLength;
    }
};