Solution Blog: (sign into leetcode to view):
https://leetcode.com/problems/longest...
Base Case:
When you hit a leaf node, return the depth and it'll propagate up the tree and be returned!
Two Paths from here:
If you went left previously, take the maximum of:
1. the current depth
2. now going down the right path, and incrementing the depth by 1
3. go down the left path AGAIN, and reset depth to 0
Then repeat this logic (but reversed) for if you went right previously.
Time complexity: O(N)
Space complexity: O(N)
On this page of the site you can watch the video online Longest ZigZag Path in a Binary Tree - LeetCode 1372 - Python Solution with a duration of hours minute second in good quality, which was uploaded by the user Code with Carter 19 April 2023, share the link with friends and acquaintances, this video has already been watched 8,138 times on youtube and it was liked by 184 viewers. Enjoy your viewing!