https://github.com/vinyl-davyl/algo-syncamore
https://github.com/vinyl-davyl/algo-syncamore
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/vinyl-davyl/algo-syncamore
- Owner: Vinyl-Davyl
- Created: 2025-01-18T05:45:58.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-01-18T05:54:36.000Z (5 months ago)
- Last Synced: 2025-01-18T06:26:36.818Z (5 months ago)
- Language: JavaScript
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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`.