LeetCode-543. Diameter of Binary Tree [C++][Java]

LeetCode-543. Diameter of Binary TreeLevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/diameter-of-binary-tree/

题目描述

Given the root of a binary tree, return the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

The length of a path between two nodes is represented by the number of edges between them.

Example 1:

Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].

Example 2:

Input: root = [1,2]
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [1, 10^4].
  • -100 <= Node.val <= 100

解题思路

【C++解法】

/**
 * 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 mNodesOfLongestPath = 0;
    
    int diameterOfBinaryTree(TreeNode* root) {
        diameter(root);
        return mNodesOfLongestPath - 1;
    }
    
    int diameter(TreeNode* root) {
        if (root == nullptr) {return 0;}
        int left = diameter(root->left) + 1;
        int right = diameter(root->right) + 1;
        mNodesOfLongestPath = max(left + right - 1, mNodesOfLongestPath);
        return max(left, right);
    }
};

用pair对象返回参数:

class Solution {
public:
    pair<int, int> diameter(TreeNode* root) {
        if (root == nullptr) {return make_pair(0, 0);}
        pair<int, int> left = diameter(root->left);
        pair<int, int> right = diameter(root->right);
        return make_pair(max(left.first, right.first) + 1,
                        max((left.first + right.first),
                           max(left.second, right.second)));
    }
    
    int diameterOfBinaryTree(TreeNode* root) {
        return diameter(root).second;
    }
};

引用参数带一个,返回值带一个:

class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        int diameter = 0;
        helper(root, diameter);
        return diameter;
    }

    int helper(TreeNode* node, int& diameter) {
        if (!node) {
            return 0;
        }
        int l = helper(node->left, diameter);
        int r = helper(node->right, diameter);
        diameter = max(l + r, diameter);
        return max(l, r) + 1;
    }
};

【Java解法】

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

使用全局变量:

class Solution {
    int mNodesOfLongestPath = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        diameter(root);
        return mNodesOfLongestPath - 1;
    }

    public int diameter(TreeNode root){
        if(root == null) return 0;
        int left = diameter(root.left) + 1;
        int right = diameter(root.right) + 1;
        mNodesOfLongestPath = Math.max(left + right - 1, mNodesOfLongestPath);
        return Math.max(left, right);
    }
}

用pair对象返回参数:

class Solution {
    class pair{
        int height;
        int diameter;
        pair(int height,int diameter){
            this.height = height;
            this.diameter = diameter;
        }
    }

    public pair diameter(TreeNode node){
        if (node == null) {return new pair(0,0);}
        pair left = diameter(node.left);
        pair right = diameter(node.right);
        return new pair(Math.max(left.height, right.height) + 1,
                        Math.max((left.height + right.height),
                                 Math.max(left.diameter, right.diameter)));
    }

    public int diameterOfBinaryTree(TreeNode root) {
        return diameter(root).diameter;
    }
}

参考文献

【1】C++ pair的基本用法总结(整理)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值