File tree 5 files changed +131
-0
lines changed
algorithms/LongestUnivaluePath
5 files changed +131
-0
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,7 @@ All solutions will be accepted!
177
177
| 836| [ Rectangle Overlap] ( https://leetcode-cn.com/problems/rectangle-overlap/description/ ) | [ java/py/js] ( ./algorithms/RectangleOverlap ) | Easy|
178
178
| 643| [ Maximum Average Subarray I] ( https://leetcode-cn.com/problems/maximum-average-subarray-i/description/ ) | [ java/py/js] ( ./algorithms/MaximumAverageSubarrayI ) | Easy|
179
179
| 189| [ Rotate Array] ( https://leetcode-cn.com/problems/rotate-array/description/ ) | [ java/py/js] ( ./algorithms/RotateArray ) | Easy|
180
+ | 687| [ Longest Univalue Path] ( https://leetcode-cn.com/problems/longest-univalue-path/description/ ) | [ java/py/js] ( ./algorithms/LongestUnivaluePath ) | Easy|
180
181
181
182
# Database
182
183
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Longest Univalue Path
2
+ This problem is easy to solve by postorder traversal
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public int longestUnivaluePath (TreeNode root ) {
12
+ int longestPath = 0 ;
13
+ Map <TreeNode , Integer > pathMap = new HashMap <TreeNode , Integer >();
14
+ LinkedList <TreeNode > stack = new LinkedList <TreeNode >();
15
+
16
+ if (root != null ) stack .addLast (root );
17
+
18
+ while (stack .size () > 0 ) {
19
+ TreeNode node = stack .getLast ();
20
+
21
+ if (node .left != null && pathMap .get (node .left ) == null ) {
22
+ stack .addLast (node .left );
23
+ } else if (node .right != null && pathMap .get (node .right ) == null ) {
24
+ stack .addLast (node .right );
25
+ } else {
26
+ stack .removeLast ();
27
+ int path = 0 ;
28
+
29
+ if (node .left != null || node .right != null ) {
30
+ int leftPath = node .left != null && node .left .val == node .val ? 1 + pathMap .get (node .left ) : 0 ,
31
+ rightPath = node .right != null && node .right .val == node .val ? 1 + pathMap .get (node .right ) : 0 ;
32
+ pathMap .put (node , Math .max (leftPath , rightPath ));
33
+ path = leftPath + rightPath ;
34
+ } else {
35
+ pathMap .put (node , 0 );
36
+ }
37
+
38
+ longestPath = Math .max (longestPath , path );
39
+ }
40
+ }
41
+
42
+ return longestPath ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @return {number }
11
+ */
12
+ var longestUnivaluePath = function ( root ) {
13
+ let longestPath = 0 ,
14
+ pathMap = new Map ( ) ,
15
+ stack = [ ]
16
+
17
+ if ( root ) stack . push ( root )
18
+
19
+ while ( stack . length > 0 ) {
20
+ let node = stack [ stack . length - 1 ]
21
+ if ( node . left && pathMap . get ( node . left ) === undefined ) {
22
+ stack . push ( node . left )
23
+ } else if ( node . right && pathMap . get ( node . right ) === undefined ) {
24
+ stack . push ( node . right )
25
+ } else {
26
+ stack . pop ( )
27
+ let path = 0
28
+
29
+ if ( node . left || node . right ) {
30
+ let leftPath = node . left && node . left . val === node . val ? 1 + pathMap . get ( node . left ) : 0 ,
31
+ rightPath = node . right && node . right . val === node . val ? 1 + pathMap . get ( node . right ) : 0
32
+ pathMap . set ( node , Math . max ( leftPath , rightPath ) )
33
+ path = leftPath + rightPath
34
+ } else {
35
+ pathMap . set ( node , 0 )
36
+ }
37
+
38
+ longestPath = Math . max ( longestPath , path )
39
+ }
40
+ }
41
+
42
+
43
+ return longestPath
44
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ def longestUnivaluePath (self , root ):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: int
13
+ """
14
+ longest_path = 0
15
+ path_map = {}
16
+ stack = []
17
+
18
+ if root : stack .append (root )
19
+
20
+ while len (stack ) > 0 :
21
+ node = stack [- 1 ]
22
+ if node .left and path_map .get (node .left ) == None :
23
+ stack .append (node .left )
24
+ elif node .right and path_map .get (node .right ) == None :
25
+ stack .append (node .right )
26
+ else :
27
+ stack .pop ()
28
+ path = 0
29
+
30
+ if node .left != None or node .right != None :
31
+ left_path = 1 + path_map .get (node .left ) if node .left != None and node .left .val == node .val else 0
32
+ right_path = 1 + path_map .get (node .right ) if node .right != None and node .right .val == node .val else 0
33
+ path_map [node ] = max (left_path , right_path )
34
+ path = left_path + right_path
35
+ else :
36
+ path_map [node ] = 0
37
+
38
+ longest_path = max (longest_path , path )
39
+
40
+ return longest_path
You can’t perform that action at this time.
0 commit comments