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(false);
14     cin.tie(NULL);
15     return 0;
16 }();
17
18 class Solution
19 {
20 public:
21     int res=0;
22     int longestUnivaluePath(TreeNode* root)
23     {
24         if(root==NULL)
25             return 0;
26         count(root);
27         return res;
28     }
29
30     int count(TreeNode* root)
31     {
32         int l=root->left?count(root->left):0;
33         int r=root->right?count(root->right):0;
34         int resl=root->left&&root->left->val==root->val?l+1:0;
35         int resr=root->right&&root->right->val==root->val?r+1:0;
36         res=max(res,resl+resr);
37         return max(resl,resr);
38     }
39 };

这个题要注意一下,容易错,辅助遍历函数有返回值。

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9160621.html

时间: 2024-11-16 00:57:57

687. Longest Univalue Path的相关文章

[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

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]Longest Univalue Path

要注意边和节点数是不一样的 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def longestUnivaluePath(self, root: TreeNode) -> int: result = [0] def backtrace(node,

[email protected] [329] Longest Increasing Path in a Matrix (DFS + 记忆化搜索)

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 diagonally o

[LeetCode]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 bounda

[LintCode] System Longest File Path

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

329. Longest Increasing Path in a Matrix

/* * 329. Longest Increasing Path in a Matrix * 2016-7-9 by Mingyang * 其实这种类型的题目没有想象的那么难,你只需要好好的按照dfs的概念一点一点的走就好了 * 记住,每个dfs不光是void的,也可以返回一个长度 */ public static final int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; public int longestIncre

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)