def test_sortedArrayToBST(nums, expected): actual = Solution().sortedArrayToBST(nums) expected = from_nums(expected) actual_queue = [actual] expected_queue = [expected] while expected_queue: actual = actual_queue.pop(0) expected = expected_queue.pop(0) assert actual.val == expected.val for queue, node in [(actual_queue, actual), (expected_queue, expected)]: if node.left: queue.append(node.left) if node.right: queue.append(node.right)
def rightSideView(self, root): if root is None: return [] stack = [(root, 0)] dic = {} res = [] while stack: node, level = stack.pop() dic[level] = node.val # add left into stack first so it will pop after right.pop, most left will override right nodes on the same level if node.left: stack.append((node.left, level + 1)) if node.right: stack.append((node.right, level + 1)) for key, value in dic.items(): res.append(value) return res test = Solution() head_node = test.sortedArrayToBST([1, 2, 3, 4, 5, 6, 7]) test1 = Solution1() print test1.rightSideView(head_node) # 4 # 2 6 # 1 3 5 7
self.res.append(self.next1()) self.res.append(self.next2()) if len(self.stack1) > 0: while self.stack1: self.res.append(self.next1()) elif len(self.stack2) > 0: while self.stack2: self.res.append(self.next2()) return self.res def next1(self): node = self.stack1.pop() x = node.right while x: self.stack1.append(x) x = x.left return node.val def next2(self): node = self.stack2.pop() x = node.right while x: self.stack2.append(x) x = x.left return node.val test = Solution() head_node1 = test.sortedArrayToBST([1, 3, 5, 7]) head_node2 = test.sortedArrayToBST([2, 3, 4, 6, 7, 8]) test1 = Solution1() print test1.getAllElements(head_node1, head_node2)
while len(self.stack1) > 0: curr = self.next1() self.res.append(curr) while len(self.stack2) > 0: curr = self.next2() self.res.append(curr) return self.res # 每次pop栈先检查curr有没有右节点,把右节点入栈并且继续弹入右节点的所有左节点 def next1(self): curr = self.stack1.pop() tmp = curr.right while tmp: self.stack1.append(tmp) tmp = tmp.left return curr.val def next2(self): curr = self.stack2.pop() tmp = curr.right while tmp: self.stack2.append(tmp) tmp = tmp.left return curr.val testBST = Solution() root1 = testBST.sortedArrayToBST([0, 1, 4, 5, 6, 8]) root2 = testBST.sortedArrayToBST([2, 3, 5, 7, 9, 10]) test = Solution1(root1, root2) print test.merge()
:type root1: TreeNode :type root2: TreeNode :rtype: bool """ if root1 is None or root2 is None: return False def getLeaf(root): stack = [root] res = [] while stack: curr = stack.pop() if not curr.left and not curr.right: res.append(curr.val) if curr.right: stack.append(curr.right) if curr.left: stack.append(curr.left) return res return True if getLeaf(root1) == getLeaf(root2) else False test = Solution() root1 = test.sortedArrayToBST([1, 2, 3, 4, 6, 9, 20]) root2 = test.sortedArrayToBST([1, 2, 3, 4, 6, 9, 10]) test1 = Solution1() print test1.leafSimilar(root1, root2)
while root: self.stack.append(root) root = root.right # @return a boolean, whether a previous number exist def hasPrev(self): return len(self.stack) > 0 # @return an integer, the previous largest number def prev(self): node = self.stack.pop() x = node.left while x: self.stack.append(x) x = x.right return node.val testBST = Solution() root = testBST.sortedArrayToBST([0, 1, 2, 3, 4, 5, 6, 7]) test = BSTPrevIterator(root) print test.prev() print test.prev() print test.prev() print test.prev() print test.prev() # 4 # 2 6 # 1 3 5 7 # 0
left1 = curr1.left right1 = curr1.right left2 = curr2.left right2 = curr2.right if left1.val == left2.val and right1.val == right2.val: stack1 = stack1 + [left1] + [right1] stack2 = stack2 + [left2] + [right2] elif left1.val == right2.val and right1.val == left2.val: stack1 = stack1 + [right1] + [left1] stack2 = stack2 + [left2] + [right2] else: return False else: return False elif not curr1 and not curr2: continue else: return False return True if not stack1 and not stack2 else False test = Solution() head_node1 = test.sortedArrayToBST([None, 2, None, 4, 5, 6, 7]) head_node2 = test.sortedArrayToBST([None, 2, None, 4, 5, 6, 7]) test1 = Solution1() print test1.flipEquiv(head_node1, head_node2) # 4 # 2 6 # 1 N 5 7
# right child could be possible if curr.val < R: queue.append(curr.right) if curr.val >= L and curr.val <= R: res += curr.val return res def rangeSumBSTBrutalForce(self, root, L, R): self.res = 0 def dfs(node, L, R): if not node: return if node.val >= L and node.val <= R: self.res += node.val dfs(node.left, L, R) dfs(node.right, L, R) dfs(root, L, R) return self.res test = Solution() head_node = test.sortedArrayToBST([0, 1, 2, 3, 4, 5, 6, 7]) test = Solution1() print test.rangeSumBST(head_node, 3, 5) # 4 # 2 6 # 1 3 5 7 # 0