|
4 | 4 |
|
5 | 5 | importjava.sql.Struct; |
6 | 6 |
|
7 | | -/** |
8 | | - * 1372. Longest ZigZag Path in a Binary Tree |
9 | | - * |
10 | | - * Given a binary tree root, a ZigZag path for a binary tree is defined as follow: |
11 | | - * Choose any node in the binary tree and a direction (right or left). |
12 | | - * If the current direction is right then move to the right child of the current node otherwise move to the left child. |
13 | | - * Change the direction from right to left or right to left. |
14 | | - * Repeat the second and third step until you can't move in the tree. |
15 | | - * Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). |
16 | | - * Return the longest ZigZag path contained in that tree. |
17 | | - * |
18 | | - * Example 1: |
19 | | - * Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1] |
20 | | - * Output: 3 |
21 | | - * Explanation: Longest ZigZag path in blue nodes (right -> left -> right). |
22 | | - * |
23 | | - * Example 2: |
24 | | - * Input: root = [1,1,1,null,1,null,null,1,1,null,1] |
25 | | - * Output: 4 |
26 | | - * Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right). |
27 | | - * |
28 | | - * Example 3: |
29 | | - * Input: root = [1] |
30 | | - * Output: 0 |
31 | | - * |
32 | | - * Constraints: |
33 | | - * Each tree has at most 50000 nodes.. |
34 | | - * Each node's value is between [1, 100]. |
35 | | - * */ |
36 | 7 | publicclass_1372 { |
37 | 8 | publicstaticclassSolution1 { |
38 | | -/**credit: https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/discuss/531808/Java-Recursion-Try-each-node-as-a-zigzag-root-then-return-valid-sum-to-parent*/ |
| 9 | +/** |
| 10 | + * credit: https://leetcode.com/problems/longest-zigzag-path-in-a-binary-tree/discuss/531808/Java-Recursion-Try-each-node-as-a-zigzag-root-then-return-valid-sum-to-parent |
| 11 | + */ |
39 | 12 | intmaxLength =0; |
40 | 13 |
|
41 | 14 | publicintlongestZigZag(TreeNoderoot) { |
|