Skip to content

Latest commit

 

History

History
240 lines (123 loc) · 20.9 KB

tree-graph.md

File metadata and controls

240 lines (123 loc) · 20.9 KB

Tree & Graph LeetCode Problems

A collection of LeetCode problems solved using Tree & Graph pattern techniques, explanations, and complexity analysis.

Problems

Easy

# Problem Language Time Space Explanation
112 Path Sum PHP O(n) O(n) Explanation

| 144 | Binary Tree Preorder Traversal | PHP | O(n) | O(n) | Explanation |

| 145 | Binary Tree Postorder Traversal | PHP | O(n) | O(n) | Explanation |

| 404 | Sum of Left Leaves | Python | O(n) | O(n) | Explanation |

| 492 | Construct the Rectangle | Python | O(n) | O(n) | Explanation |

| 559 | Maximum Depth of N-ary Tree | Python | O(n) | O(n) | Explanation |

| 566 | Reshape the Matrix | Python | O(n) | O(n) | Explanation |

| 589 | N-ary Tree Preorder Traversal | Python | O(n) | O(n) | Explanation |

| 590 | N-ary Tree Postorder Traversal | Python | O(n) | O(n) | Explanation |

| 696 | Count Binary Substrings | Python | O(n) | O(n) | Explanation |

| 766 | Toeplitz Matrix | Python | O(n) | O(n) | Explanation |

| 867 | Transpose Matrix | Python | O(n) | O(n) | Explanation |

| 1022 | Sum of Root To Leaf Binary Numbers | Python | O(n) | O(n) | Explanation |

| 1030 | Matrix Cells in Distance Order | Python | O(n) | O(n) | Explanation |

| 1160 | Find Words That Can Be Formed by Characters | Python | O(n) | O(n) | Explanation |

| 1252 | Cells with Odd Values in a Matrix | Python | O(n) | O(n) | Explanation |

| 1260 | Shift 2D Grid | Racket | O(n) | O(n) | Explanation |

| 1299 | Replace Elements with Greatest Element on Right Side | Python | O(n) | O(n) | Explanation |

| 1337 | The K Weakest Rows in a Matrix | Python | O(n) | O(n) | Explanation |

| 1351 | Count Negative Numbers in a Sorted Matrix | Python | O(n) | O(n) | Explanation |

| 1380 | Lucky Numbers in a Matrix | Python | O(n) | O(n) | Explanation |

| 1496 | Path Crossing | Python | O(n) | O(n) | Explanation |

| 1572 | Matrix Diagonal Sum | Python | O(n) | O(n) | Explanation |

| 1688 | Count of Matches in Tournament | C++ | O(n) | O(n) | Explanation |

| 1710 | Maximum Units on a Truck | C++ | O(n) | O(n) | Explanation |

| 1758 | Minimum Changes To Make Alternating Binary String | C++ | O(n) | O(n) | Explanation |

| 1779 | Find Nearest Point That Has the Same X or Y Coordinate | C++ | O(n) | O(n) | Explanation |

| 1854 | Maximum Population Year | C++ | O(n) | O(n) | Explanation |

| 1886 | Determine Whether Matrix Can Be Obtained By Rotation | C++ | O(n) | O(n) | Explanation |

| 1967 | Number of Strings That Appear as Substrings in Word | Kotlin | O(n) | O(n) | Explanation |

| 2667 | Create Hello World Function | TypeScript | O(n) | O(n) | Explanation |

| 2884 | Modify Columns | Python | O(n) | O(n) | Explanation |

| 2885 | Rename Columns | Python | O(n) | O(n) | Explanation |

| 2889 | Reshape Data: Pivot | Python | O(n) | O(n) | Explanation |

| 3345 | Smallest Divisible Digit Product I | Ruby | O(n) | O(n) | Explanation |

| 3870 | Count Commas in Range | JavaScript | O(n) | O(n) | Explanation |

| 3903 | Smallest Stable Index I | JavaScript | O(n) | O(n) | Explanation |

Medium

# Problem Language Time Space Explanation
6 Zigzag Conversion Python O(n) O(n) Explanation

| 38 | Count and Say | Python | O(n) | O(n) | Explanation |

| 50 | Pow(x, n) | Python | O(n) | O(n) | Explanation |

| 53 | Maximum Subarray | Python | O(n) | O(n) | Explanation |

| 54 | Spiral Matrix | Python | O(n) | O(n) | Explanation |

| 59 | Spiral Matrix II | Python | O(n) | O(n) | Explanation |

| 64 | Minimum Path Sum | Python | O(n) | O(n) | Explanation |

| 71 | Simplify Path | Python | O(n) | O(n) | Explanation |

| 73 | Set Matrix Zeroes | JavaScript | O(n) | O(n) | Explanation |

| 74 | Search a 2D Matrix | Python | O(n) | O(n) | Explanation |

| 102 | Binary Tree Level Order Traversal | JavaScript | O(n) | O(n) | Explanation |

| 103 | Binary Tree Zigzag Level Order Traversal | JavaScript | O(n) | O(n) | Explanation |

| 105 | Construct Binary Tree from Preorder and Inorder Traversal | JavaScript | O(n) | O(n) | Explanation |

| 106 | Construct Binary Tree from Inorder and Postorder Traversal | JavaScript, Python | O(n) | O(n) | Explanation |

| 129 | Sum Root to Leaf Numbers | JavaScript | O(n) | O(n) | Explanation |

| 133 | Clone Graph | JavaScript | O(n) | O(n) | Explanation |

| 198 | House Robber | JavaScript | O(n) | O(n) | Explanation |

| 204 | Count Primes | JavaScript, Python | O(n) | O(n) | Explanation |

| 240 | Search a 2D Matrix II | Python | O(n) | O(n) | Explanation |

| 427 | Construct Quad Tree | JavaScript | O(n) | O(n) | Explanation |

| 437 | Path Sum III | JavaScript | O(n) | O(n) | Explanation |

| 550 | Game Play Analysis IV | Python | O(n) | O(n) | Explanation |

| 670 | Maximum Swap | Python | O(n) | O(n) | Explanation |

| 684 | Redundant Connection | Python | O(n) | O(n) | Explanation |

| 687 | Longest Univalue Path | Python | O(n) | O(n) | Explanation |

| 690 | Employee Importance | Python | O(n) | O(n) | Explanation |

| 877 | Stone Game | C++ | O(n) | O(n) | Explanation |

| 918 | Maximum Sum Circular Subarray | JavaScript | O(n) | O(n) | Explanation |

| 1306 | Jump Game III | C++ | O(n) | O(n) | Explanation |

| 1358 | Number of Substrings Containing All Three Characters | Java | O(n) | O(n) | Explanation |

| 1466 | Reorder Routes to Make All Paths Lead to the City Zero | JavaScript | O(n) | O(n) | Explanation |

| 1514 | Path with Maximum Probability | Python | O(n) | O(n) | Explanation |

| 1631 | Path With Minimum Effort | Python | O(n) | O(n) | Explanation |

| 1914 | Cyclically Rotating a Grid | C++ | O(n) | O(n) | Explanation |

| 2029 | Stone Game IX | C++ | O(n) | O(n) | Explanation |

| 2101 | Detonate the Maximum Bombs | Python | O(n) | O(n) | Explanation |

| 2161 | Partition Array According to Given Pivot | Java | O(n) | O(n) | Explanation |

| 2492 | Minimum Score of a Path Between Two Cities | Java | O(n) | O(n) | Explanation |

| 2624 | Snail Traversal | TypeScript | O(n) | O(n) | Explanation |

| 2685 | Count the Number of Complete Components | Java | O(n) | O(n) | Explanation |

| 2812 | Find the Safest Path in a Grid | Java | O(n) | O(n) | Explanation |

| 3286 | Find a Safe Walk Through a Grid | Java | O(n) | O(n) | Explanation |

| 3310 | Remove Methods From Project | C++ | O(n) | O(n) | Explanation |

| 3513 | Number of Unique XOR Triplets I | C++ | O(n) | O(n) | Explanation |

| 3517 | Smallest Palindromic Rearrangement I | C++ | O(n) | O(n) | Explanation |

| 3532 | Path Existence Queries in a Graph I | Java | O(n) | O(n) | Explanation |

| 3537 | Fill a Special Grid | Python | O(n) | O(n) | Explanation |

| 3558 | Number of Ways to Assign Edge Weights I | Java | O(n) | O(n) | Explanation |

| 3612 | Process String with Special Operations I | Java | O(n) | O(n) | Explanation |

| 3727 | Maximum Alternating Sum of Squares | JavaScript | O(n) | O(n) | Explanation |

| 3742 | Maximum Path Score in a Grid | C++ | O(n) | O(n) | Explanation |

Hard

# Problem Language Time Space Explanation
51 N-Queens Python O(n) O(n) Explanation

| 124 | Binary Tree Maximum Path Sum | JavaScript | O(n) | O(n) | Explanation |

| 329 | Longest Increasing Path in a Matrix | Python | O(n) | O(n) | Explanation |

| 466 | Count The Repetitions | Python | O(n) | O(n) | Explanation |

| 587 | Erect the Fence | Python | O(n) | O(n) | Explanation |

| 1368 | Minimum Cost to Make at Least One Valid Path in a Grid | Python | O(n) | O(n) | Explanation |

| 1825 | Finding MK Average | Python | O(n) | O(n) | Explanation |

| 1840 | Maximum Building Height | Python | O(n) | O(n) | Explanation |

| 1923 | Longest Common Subpath | Python | O(n) | O(n) | Explanation |

| 2045 | Second Minimum Time to Reach Destination | Python | O(n) | O(n) | Explanation |

| 2179 | Count Good Triplets in an Array | JavaScript | O(n) | O(n) | Explanation |

| 2736 | Maximum Sum Queries | JavaScript | O(n) | O(n) | Explanation |

| 2867 | Count Valid Paths in a Tree | JavaScript | O(n) | O(n) | Explanation |

| 3161 | Block Placement Queries | Python | O(n) | O(n) | Explanation |

| 3515 | Shortest Path in a Weighted Tree | JavaScript | O(n) | O(n) | Explanation |

| 3518 | Smallest Palindromic Rearrangement II | PHP | O(n) | O(n) | Explanation |

| 3534 | Path Existence Queries in a Graph II | PHP | O(n) | O(n) | Explanation |

| 3559 | Number of Ways to Assign Edge Weights II | Python | O(n) | O(n) | Explanation |

| 3614 | Process String with Special Operations II | Python | O(n) | O(n) | Explanation |

| 3620 | Network Recovery Pathways | PHP | O(n) | O(n) | Explanation |

| 3700 | Number of ZigZag Arrays II | PHP | O(n) | O(n) | Explanation |

| 3841 | Palindromic Path Queries in a Tree | Python | O(n) | O(n) | Explanation |

| 3864 | Minimum Cost to Partition a Binary String | Python | O(n) | O(n) | Explanation |