687. Longest Univalue Path
Last updated
Last updated
้้กๅ ถๅฏฆๅฐฑๆฏ base on
543 Diameter of Binary Tree (post order
https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-66994/dong-ge-da-334dd/
ๅๅ ไธ่ฆๆชขๆฅ prev val ไธๆจฃๅฐฑๅฏไปฅ
ๅ ็บๆ้ท็ path ๆๅฏ่ฝๅบ็พๅจๆๅ่ฝๆ็้่ฃก
ๆไปฅๆฑๅทฆ๏ผๅณ ๆๅคง Math.max(left+right, result)
ไฝๆๅพ ๏ผ็ถๅ็ตๆๅ ๅซๆฌ่บซ็้ป, ไธๆฏ path
return max(left, right)+1
ไปฅไธ้ๅๆณๆฏไปฅๆฑ็ฏ้ป(ๅ ไธ้ฃๅๆ้ป)็่งๅบฆๅป็็...ๆไปฅ+1
ไฝๆๅพ-1
T: O(n)
S: O(h), worst O(n)