An open API service indexing awesome lists of open source software.

https://github.com/vinyl-davyl/algo-syncamore


https://github.com/vinyl-davyl/algo-syncamore

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

        

### Breakdown of Solution

Finding the longest zigzag path in a binary tree. A zigzag path alternates between left and right child nodes as we traverse the tree.

### **Approach**

1. **Recursive Traversal**:
- Used a helper function to traverse the binary tree recursively.
- At each node, tracked:
- The direction we came from (left or right).
- The current length of the zigzag path.

2. **Key Logic**:
- If the previous direction was **left**, the next step in the zigzag must go **right** (and vice versa).
- If the direction alternates, we increment the zigzag path length.
- If the direction doesn't alternate, we reset the zigzag path length to `0`.

3. **Base Case**:
- If the current node is `null`, stop the traversal.

4. **Tracking the Longest Path**:
- Updated a global variable `maxZigzag` to store the longest zigzag path length encountered during the traversal.

5. **Starting Point**:
- At the root node, we considered both possible directions (left and right) and begin the traversal.

### **Why This Works**
- The recursive function ensures that every possible zigzag path is explored.
- The `maxZigzag` variable tracks the longest zigzag path efficiently across all recursive calls.

### **Efficiency**
- Time Complexity: **O(N)**, where N is the number of nodes in the tree, as we visit each node once.
- Space Complexity: **O(H)**, where H is the height of the tree, due to the recursion stack.

### **Result**
Identified the longest zigzag path as `2`.