Solved LeetCode program · 994 of 1,000
#1372 · Longest ZigZag Path in a Binary Tree
Original English source
Keep a shared values holding 1, 1, 1, 1, 1, 1, 1, and 1. Keep a shared lefts holding -1, 2, -1, 4, -1, -1, -1, and -1. Keep a shared rights holding 1, 3, -1, 5, 6, -1, 7, and -1. Keep a shared best of 0.
To walk from node (a whole number) with direction (a whole number) and length (a whole number). If node is less than 0, return. If length is more than best, set best to length. The left is item node of lefts. The right is item node of rights. The leftlength is 1. If direction is 1, set leftlength to length plus 1. The rightlength is 1. If direction is 0, set rightlength to length plus 1. Walk from left with 0 and leftlength. Walk from right with 1 and rightlength.
Walk from 0 with 0 and 0. Show best.
Expected stdout
3