Tree的操作我喜欢用recursion,将子结点看成新的子树进行递归。 eg11: 107. Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 这个问题没有用递归,而是采用queue...
// case 2: last element (n) is seleced, so pick k-1 elements out from n-1 totals => recursion exit 2 List<List<Integer>> subList2 = combine(n-1, k-1); // then add the last element for (List<Integer> l : subList2) { l.add(n); subList1.add(l); // merge into subList...
Find the Missing Number (easy) Find all Missing Numbers (easy) Find the Duplicate Number (easy) Find all Duplicate Numbers (easy) 6. Pattern: In-place Reversal of a LinkedList,链表翻转 在众多问题中,题目可能需要你去翻转链表中某一段的节点。通常,要求都是你得原地翻转,就是重复使用这些已经建好的...
// Solution 1: This problem can be solved withoutrecursion, not so difficult. But it's easy to write sloppy code. A perfect kill is very challenging. 代码1 //Code 1 157 Read N Characters Given Read4 // #157 用Read4读取N个字符 描述:从前有个API叫read4,能用来读4个字符。请用其读取N...
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1. 【解答】checkBalanced方法用来递归检查是否平衡的,返回树高度,偷了个巧,如果发现不平衡抛出TreeUnbalancedException异常: ...
The recursion mainly breaks down elegantly to the following two cases:主要考虑两种递归情况1. If the next character of p is NOT ‘*’, then it must match the current character of s. Continue pattern matching with the next character of both s and p. 2. If the next character of p is ‘...
Middle of the LinkedList (easy) 4. Pattern: Merge Intervals,区间合并类型 区间合并模式是一个用来处理有区间重叠的很高效的技术。在设计到区间的很多问题中,通常咱们需要要么判断是否有重叠,要么合并区间,如果他们重叠的话。这个模式是这么起作用的:
It seems an easy problem first using recursion and brute forcefully solving the problem, but the OJ will quickly throw a TLE (Time Limit Exceeded) error since it is exponential time. Well, if you were like me, tried really hard to optimize my brute force solution, either using some heurist...
938 938. Range Sum of BST.java Easy [BST, Recursion, Tree] Java 500 210 210. Course Schedule II.java Medium [BFS, DFS, Graph, Topological Sort] O(n) O(n) Java 501 68 68. Text Justification.java Hard [Enumeration, String] O(n) go over words O(maxLength) buffer list Java 502 31...
938 938. Range Sum of BST.java Easy [BST, Recursion, Tree] Java 500 210 210. Course Schedule II.java Medium [BFS, DFS, Graph, Topological Sort] O(n) O(n) Java 501 68 68. Text Justification.java Hard [Enumeration, String] O(n) go over words O(maxLength) buffer list Java 502 31...