tags: leetcode
687. Longest Univalue Path
https://leetcode.com/problems/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.
The length of path between two nodes is represented by the number of edges between them.
example 3:
1
/ \
1 1
/ \ \
1 1 1
/\ \
1 2 1
example 4:
1
/ \
1 1
/ \ \
1 1 1
[1 1 1 1 1 1]
- 左邊的路徑、右邊的路徑
- 路徑怎麼決定?
如果能接過來(右邊的值=當前的值): 右邊的路徑+1
如果不能:0
Remark
http://www.csie.ntnu.edu.tw/~u91029/BinaryTree.html
1 | # Definition for a binary tree node. |