LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

题目:

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

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

Example 1:

Input:

              5
             /             4   5
           / \             1   1   5

Output: 2

Example 2:

Input:

              1
             /             4   5
           / \             4   4   5

Output: 2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

分析:

给定一颗二叉树,求最长的路径,其中路径是相同节点间的边数。

递归求解此问题,对于每一个结点,我们要求此时最大的路径数,而最大路径数是由其左右两个孩子结点决定的,那么递归的终止条件就是当为空结点时,路径数为0,当我们知道左右孩子的最大路径数时,需要判断当前和左右孩子结点是否相同,如果相同则当前的结点的最大路径数应该为左孩子路径数+1/右孩子路径数+1取最大值,同时更新结果为当前结果和以当前结点根结点时的左右孩子路径数的和的最大值。

程序:

C++

/**
 * 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 longestUnivaluePath(TreeNode* root) {
        int res = 0;
        if(root == nullptr)
            return res;
        univaluePath(root, res);
        return res;
    }
private:
    int univaluePath(TreeNode* root, int& res){
        if(root == nullptr)
            return 0;
        int l = univaluePath(root->left, res);
        int r = univaluePath(root->right, res);
        int pl = 0;
        int pr = 0;
        if(root->left != nullptr && root->val == root->left->val)
            pl = l + 1;
        if(root->right != nullptr && root->val == root->right->val)
            pr = r + 1;
        res = max(res, pl + pr);
        return max(pl, pr);
    }
};

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int longestUnivaluePath(TreeNode root) {
        if(root == null)
            return res;
        univaluePath(root);
        return res;
    }
    private int res = 0;
    private int univaluePath(TreeNode root){
        if(root == null)
            return 0;
        int l = univaluePath(root.left);
        int r = univaluePath(root.right);
        int pl = 0;
        int pr = 0;
        if(root.left != null && root.val == root.left.val)
            pl = l + 1;
        if(root.right != null && root.val == root.right.val)
            pr = r + 1;
        res = Math.max(res, pl + pr);
        return Math.max(pl, pr);
    }

}

原文地址:https://www.cnblogs.com/silentteller/p/12353285.html

时间: 2024-10-08 22:50:32

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)的相关文章

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

[LeetCode] 687. Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

687. Longest Univalue Path

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 11 static int wing=[]() 12 { 13 std::ios::sync_with_stdio

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

[LeetCode][JavaScript]Longest Increasing Path in a Matrix

Longest Increasing Path in a Matrix Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary

Leetcode 687.最长同值路径

最长同值路径 给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 输出: 2 示例 2: 输入: 输出: 2 注意: 给定的二叉树不超过10000个结点. 树的高度不超过1000. 思路 Intuition We can think of any path (of nodes with the same values) as up to two arrows extendi

【Leetcode】Longest Increasing Path in a Matrix

题目链接:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ 题目: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move dia

[C++]LeetCode: 99 Longest Palindromic Substring (最长回文子串)

题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 思路:题目要求的s的一个最长回文子串.暴力解决办法就是枚举所有的子串,再对每个子串进行回文判断.进行剪枝,我们考虑可以使用动态规划来避免重复的判

LeetCode #329. Longest Increasing Path in a Matrix

题目 Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed)