# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:deflongestZigZag(self, root: TreeNode) ->int:ifroot ==None:return0sel
Can you solve this real interview question? Longest ZigZag Path in a Binary Tree - You are given the root of a binary tree. A ZigZag path for a binary tree is defined as follow: * Choose any node in the binary tree and a direction (right or left). *
噫DFS好,用一个boolean控制向左向右,二叉树还是太菜了我 https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/discuss/534418/Java-DFS-Solution-with-comment-O(N)-Clean-code