Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-04-19 16:11:36
1372. Longest ZigZag Path in a Binary Tree
給你一個二元樹,找出這個二元樹裡的Z字型路徑最大長度。
Example :
https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png
Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
Output: 3
Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png
Input: root = [1,1,1,null,1,null,null,1,1,null,1]
Output: 4
Explanation: Longest ZigZag path in blue nodes (left -> right -> left ->
right).
思路:
1.用dfs遍歷樹,把上一層的最大Z字型長度按照方向交錯傳給下一層,並在途中不斷更
新 ans。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com