Skip to content

ethan-k/leet-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

LEETCODE

Cheat Sheet

Binary Tree Traversals

Time complexity: O(n), n = size(tree)

Java (preorder, inorder, postorder)

void preorder(TreeNode root) {
   if (root == null) {
        return
   }
    System.out.println(root.value)
    preorder(root.left)
    preorder(root.right)
}


void inorder(TreeNode root) {
    if (root == null) {
        return
    }
    inorder(root.left)
    System.out.println(root.value)
    inorder(root.right)
}

void postorder(TreeNode root) {
    if (root == null) {
        return
    }
    postorder(root.left)
    postorder(root.right)
    System.out.println(root.value)
}

Kotlin (preorder, inorder, postorder)

fun preorder(root: TreeNode?) {
   if (root == null) {
        return
   }
    println(root.value)
    preorder(root.left)
    preorder(root.right)
}


fun inorder(root: TreeNode?) {
    if (root == null) {
        return
    }
    inorder(root.left)
    println(root.value)
    inorder(root.right)
}

fun postorder(root: TreeNode?) {
    if (root == null) {
        return
    }
    postorder(root.left)
    postorder(root.right)
    println(root.value)
}

Solved

  1. Hamming Distance
  2. Judge Route Circle
  3. FizzBuzz
  4. MaxConsecutiveOnes
  5. MaximumDepthofBinaryTree
  6. NextGreaterElementOne
  7. ReshapetheMatrix
  8. ReverseLinkedList
  9. ImageSmoother
  10. Find All Duplicates in an Array
  11. Ransom Note
  12. Count Primes
  13. Two Sum
  14. Replace Words
  15. Linked List Cycle
  16. Plus One
  17. Power of Four
  18. Merge Two Sorted Lists
  19. Number of 1 Bits
  20. LongestUncommonSubsequence
  21. Majority Element
  22. Degree of an Array
  23. ExcelSheetColumnNumber
  24. Detect Capital
  25. SelfDividingNumbers
  26. 1-bit and 2-bit Characters
  27. Add Digits
  28. Employee Importance
  29. Invert Binary Tree
  30. Binary Number with Alternating Bits
  31. LRU Cache
  32. Power of Two
  33. Move Zeroes
  34. Flood Fill
  35. Range Addition II
  36. Find the Difference
  37. Construct String from Binary Tree
  38. Maximum Binary Tree
  39. Complex Number Multiplication
  40. Intersection of Two Arrays
  41. Binary Tree Tilt
  42. Base 7
  43. SumOfLeftLeaves
  44. Power of Three
  45. Find Pivot Index
  46. Search Insert Position
  47. Roman to Integer
  48. Valid Anagram
  49. Teemo Attacking
  50. Reverse Integer
  51. Longest Common Prefix
  52. Counting Bits
  53. Reverse Vowels of a String
  54. Is Subsequence
  55. Min Stack
  56. Find Largest Value in Each Tree Row
  57. Kth Smallest Element in a BST
  58. Daily Temperatures
  59. Two Sum IV - Input is a BST
  60. Single Number
  61. Student Attendance Record I
  62. Same Tree
  63. Assign Cookies
  64. Construct the Rectangle
  65. Jewels and Stones
  66. Unique Morse Code Words
  67. Number of Lines To Write String
  68. Shortest Distance to a Character
  69. Merge Two Binary Trees
  70. Subdomain Visit Count
  71. Island Perimeter
  72. Partition Labels
  73. Max Increase to Keep City Skyline
  74. Binary Search Tree to Greater Sum Tree
  75. Insert into a Binary Search Tree
  76. Defanging an IP Address
  77. Range Sum of BST
  78. To Lower Case
  79. Find Common Characters
  80. K-diff Pairs in an Array
  81. Available Captures for Rook
  82. Occurrences After Bigram
  83. Minimum Add to Make Parentheses Valid
  84. Fibonacci Number
  85. Number of Recent Calls
  86. DI String Match
  87. Middle of the Linked List
  88. Contains Duplicate
  89. Distance Between Bus Stops
  90. Letter Tile Possibilities
  91. Unique Paths
  92. Unique Number of Occurrences
  93. SortArrayByParity
  94. Sort Array By Parity II
  95. N-th Tribonacci Number
  96. N-ary Tree Level Order Traversal
  97. Reverse Only Letters
  98. Split a String in Balanced Strings
  99. Delete Columns to Make Sorted
  100. Lemonade Change
  101. Maximize Sum Of Array After K Negations
  102. Maximum Depth of N-ary Tree
  103. Distribute Candies to People
  104. Cells with Odd Values in a Matrix
  105. Search in a Binary Search Tree
  106. N-ary Tree Postorder Traversal
  107. N-ary Tree Preorder Traversal
  108. Letter Case Permutation
  109. Find Bottom Left Tree Value
  110. Matrix Cells in Distance Order
  111. Maximum Number of Balloons
  112. Complement of Base 10 Integer
  113. Positions of Large Groups
  114. ClimbingStairs
  115. Find the Town Judge
  116. Valid Parentheses
  117. Remove Element
  118. Add Two Numbers
  119. Longest Palindrome
  120. Longest Substring Without Repeating Characters
  121. Integer to Roman
  122. Container With Most Water
  123. Palindrome Number
  124. Last Stone Weight
  125. Minimum Path Sum
  126. Trapping Rain Water
  127. Number of Islands
  128. 3Sum
  129. 3Sum Closest
  130. MaximumSubArray
  131. Minimum Size Subarray Sum
  132. Flatten Nested List Iterator
  133. Palindrome Permutation
  134. Max Stack
  135. Merge k Sorted Lists
  136. Car Pooling
  137. Meeting Rooms II
  138. Subarray Sum Equals K
  139. Most Common Word
  140. Top K Frequent Elements
  141. String Compression
  142. K Closest Points to Origin
  143. Minimum Remove to Make Valid Parentheses
  144. Cousins in Binary Tree
  145. Kids With the Greatest Number of Candies
  146. Count Square Submatrices with All Ones
  147. Maximal Square
  148. Subtract the Product and Sum of Digits of an Integer
  149. Two Sum II - Input array is sorted
  150. House Robber
  151. Max Area of Island
  152. Binary Search Tree Iterator
  153. Add Two Numbers II
  154. Binary Tree Zigzag Level Order Traversal
  155. Shuffle the Array
  156. Remove Vowels from a String
  157. Number of Steps to Reduce a Number to Zero
  158. Single-Row Keyboard
  159. Maximum Subarray
  160. Running Sum of 1d Array
  161. Create Target Array in the Given Order
  162. Find All The Lonely Nodes
  163. Find N Unique Integers Sum up to Zero
  164. Target Sum
  165. Relative Sort Array
  166. Univalued Binary Tree
  167. High Five
  168. Longest Palindromic Subsequence
  169. Nested List Weight SumNested List Weight Sum
  170. XOR Operation in an Array
  171. Minimum Time Visiting All Points
  172. How Many Numbers Are Smaller Than the Current Number
  173. Find Numbers with Even Number of Digits
  174. Decompress Run-Length Encoded List
  175. Path Sum
  176. SumOfDigitsInTheMinimumNumber
  177. Palindrome Linked List
  178. Palindromic Substrings
  179. Moving Average from Data Stream
  180. Contains Duplicate II
  181. Time Based Key-Value Store
  182. Lowest Common Ancestor of a Binary Tree
  183. Binary Tree Maximum Path Sum
  184. Find Minimum in Rotated Sorted Array
  185. Find First and Last Position of Element in Sorted Array
  186. Word Search II
  187. Search a 2D Matrix II
  188. Binary Tree Inorder Traversal]
  189. Insert Interval
  190. Sort List
  191. Remove Duplicates from Sorted Array II
  192. First Unique Character in a String
  193. Largest Rectangle in Histogram
  194. Next Permutation
  195. Missing Number
  196. Shortest Unsorted Continuous Subarray
  197. House Robber III
  198. Implement Trie (Prefix Tree)
  199. Rotate Image
  200. Binary Tree Level Order Traversal
  201. Sort Colors
  202. Intersection of Two Linked Lists
  203. Binary Tree Vertical Order Traversal
  204. NQueen
  205. Logger Rate Limiter
  206. Find Duplicate File in System
  207. Valid Palindrome
  208. Add and Search Word - Data structure design
  209. Closest Binary Search Tree Value
  210. Path Sum II
  211. Path Sum III
  212. Best Time to Buy and Sell Stock II
  213. Best Time to Buy and Sell Stock with Cooldown
  214. Find All Numbers Disappeared in an Array
  215. Convert Sorted Array to Binary Search Tree
  216. Backspace String Compare Easyme-to-buy-and-sell-stock-with-cooldown/)
  217. Game of Life
  218. Distribute Candies to People
  219. Friend Circles
  220. Robot Bounded In Circle
  221. Clone Graph
  222. Remove Duplicates from Sorted Array
  223. Remove Linked List Elements
  224. Remove All Adjacent Duplicates In String
  225. Remove All Adjacent Duplicates in String II
  226. Find Anagram Mappings
  227. Average of Levels in Binary Tree
  228. Hit Counter
  229. Binary Tree Right Side View
  230. Binary Tree Paths
  231. Convert Sorted List to Binary Search Tree
  232. Word Pattern
  233. All Elements in Two Binary Search Trees
  234. Sum of Root To Leaf Binary Number
  235. Bulls And Cows
  236. Walls and Gates
  237. Spiral Matrix II
  238. Merge Intervals
  239. Maximum Length of Repeated Subarray
  240. Inorder Successor in BST
  241. Inorder Successor in BST Two
  242. Count And Say
  243. Implement strStr()
  244. Rotate Array
  245. Unique Paths III
  246. Vertical Order Traversal of a Binary Tree
  247. Car Pooling
  248. Swap Nodes in Pairs
  249. Transpose Matrix
  250. Gas Station
  251. LFU Cache
  252. Permutation in String
  253. Smallest Subtree with all the Deepest Nodes
  254. Minimum Depth of Binary Tree
  255. Longest Substring with At Most K Distinct Characters
  256. Intersection of Two Arrays II
  257. WordBreak
  258. Letter Combinations of a Phone Number
  259. Perform String Shifts
  260. Group Shifted Strings
  261. Remove Covered Intervals
  262. Maximum Distance in Arrays
  263. Rotate List
  264. Binary Search
  265. Two Sum III - Data structure design
  266. Minimum Number of Arrows to Burst Balloons
  267. Buddy Strings
  268. House Robber II
  269. Search a 2D Matrix
  270. Repeated DNA Sequences
  271. Armstrong Number
  272. Lowest Common Ancestor of a Binary Search Tree
  273. Minimum Domino Rotations For Equal Row
  274. Minimum Domino Rotations For Equal Row
  275. Search in a Sorted Array of Unknown Size
  276. Consecutive Characters
  277. Meeting Rooms
  278. Two Sum Less Than K
  279. Valid Mountain Array
  280. Reorganize String
  281. Binary Gap
  282. Network Delay Time
  283. Unique Paths II
  284. Remove Duplicates from Sorted List II
  285. Count Sorted Vowel Strings
  286. Tuple with Same Product
  287. Find if Path Exists in Graph
  288. Number of Operations to Make Network Connected
  289. Find Center of Star Graph
  290. All Paths From Source to Target
  291. Minimize Maximum Pair Sum in Array
  292. Deepest Leaves Sum
  293. Balance a Binary Search Tree
  294. Jump Game III
  295. Remove Nth Node From End of List
  296. Reverse Bits
  297. Minimum Operations to Make the Array Increasing
  298. Minimum Cost to Move Chips to The Same Position
  299. Spiral Matrix
  300. Concatenation of Array
  301. Minimum Number of Operations to Move All Balls to Each Box
  302. Watering Plants
  303. Partitioning Into Minimum Number Of Deci-Binary Numbers

To Solve

  1. Permutations

Failed

  1. PermutationInString

Baekjoon

  1. ATM
  2. Compare two numbers

About

Leet Code Practice

Resources

Stars

Watchers

Forks