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 extending from it‘s root.

Specifically, the root of a path will be the unique node such that the parent of that node does not appear in the path, and an arrow will be a path where the root only has one child node in the path.

Then, for each node, we want to know what is the longest possible arrow extending left, and the longest possible arrow extending right? We can solve this using recursion.

Algorithm

Let arrow_length(node) be the length of the longest arrow that extends from the node. That will be 1 + arrow_length(node.left) if node.left exists and has the same value as node. Similarly for the node.right case.

While we are computing arrow lengths, each candidate answer will be the sum of the arrows in both directions from that node. We record these candidate answers and return the best one.

 1 class Solution {
 2     int ans;
 3     public int longestUnivaluePath(TreeNode root) {
 4         ans = 0;
 5         arrowLength(root);
 6         return ans;
 7     }
 8     public int arrowLength(TreeNode node) {
 9         if (node == null) return 0;
10         int left = arrowLength(node.left)
11         int right = arrowLength(node.right);
12         int arrowLeft = 0, arrowRight = 0;
13         if (node.left != null && node.left.val == node.val) {
14             arrowLeft += left + 1;
15         }
16         if (node.right != null && node.right.val == node.val) {
17             arrowRight += right + 1;
18         }
19         ans = Math.max(ans, arrowLeft + arrowRight);
20         return Math.max(arrowLeft, arrowRight);
21     }
22 }

原文地址:https://www.cnblogs.com/kexinxin/p/10400351.html

时间: 2024-09-30 21:43:12

Leetcode 687.最长同值路径的相关文章

687. 最长同值路径

给定一个二叉树,找到最长的路径,这个路径中的每个节点具有相同值. 这条路径可以经过也可以不经过根节点. 注意:两个节点之间的路径长度由它们之间的边数表示. 示例 1: 输入: 5 / \ 4 5 / \ \ 1 1 5输出: 2 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/longest-univalue-path 1 package leetCode.binaryTree; 2 public class LongestUnivalueP

[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 最长同值路径 (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. Exam

【LeetCode刷题】最长同值路径:妙解

妙啊妙,学一下,最大树路径的题 原文地址:https://www.cnblogs.com/xukaiae86/p/12047329.html

Leetcode687. 最长同值路径

这个路径可能存在从子节点经过父节点,再到子节点的情况,所有从当前节点构成的路径需要考虑左右两条路径相加,用递归,求得左右的最长路径,相加,即为所求 // Study.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <vector> #include <unordered_map> #include <unordered_set> #includ

[luogu] P4551 最长异或路径(贪心)

P4551 最长异或路径 题目描述 给定一棵\(n\)个点的带权树,结点下标从\(1\)开始到\(N\).寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或. 输入输出格式 输入格式: 第一行一个整数\(N\),表示点数. 接下来 \(n-1\) 行,给出 \(u,v,w\) ,分别表示树上的 \(u\) 点和 \(v\) 点有连边,边的权值是 \(w\). 输出格式: 一行,一个整数表示答案. 输入输出样例 输入样例#1: 复制 4 1 2 3 2

Leetcode:Minimum Path Sum 矩形网格最小路径和

Minimum Path Sum: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 解题分析: 每次只能向下或者向

建筑群最长坡值

建筑群最长坡值 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte 总提交:454            测试通过:168 描述 建筑群所有建筑高度分别为h1.h2-hN,可以得到一些单调递减的序列hi1.hi2-hiK,其长度称为建筑群的坡值,这里1≤i1< i2<-< iK≤N. 你的任务:对于给定的建筑群所有建筑高度,求出建筑群最长坡值. 输入 第一行是建筑群中的建筑数N(1≤N≤1000). 第二行依次给出各个建筑的高度(

LeetCode:最长公共前缀【14】

LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存