Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb74ccd

Browse files
refactor 1372
1 parent8c3647b commitdb74ccd

File tree

1 file changed

+3
-30
lines changed

1 file changed

+3
-30
lines changed

‎src/main/java/com/fishercoder/solutions/_1372.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,11 @@
44

55
importjava.sql.Struct;
66

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-
* */
367
publicclass_1372 {
378
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+
*/
3912
intmaxLength =0;
4013

4114
publicintlongestZigZag(TreeNoderoot) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp