level = 0 while len(current) > 0: future = [] temp = [] for i in range(len(current)): node = current[i] if node: temp.append(node.val) future.append(node.left) future.append(node.right) if temp and level % 2 == 0: ans.append(temp) elif temp and level % 2 == 1: ans.append((temp[-1::-1])) current = future level += 1 return ans codec = Codec() root = codec.deserialize("3,9,20,None,None,15,7") assert Solution().zigzagLevelOrder(root) == [[3], [20, 9], [15, 7]]
def itr(root): if not root: return itr(root.left) self.lst.append(root.val) itr(root.right) itr(root) def next(self) -> int: """ @return the next smallest number """ return self.lst.pop(0) def hasNext(self) -> bool: """ @return whether we have a next smallest number """ return len(self.lst) > 0 root = Codec().deserialize("7,3,15,None,None,9,20") # Your BSTIterator object will be instantiated and called as such: obj = BSTIterator(root) v = [] while obj.hasNext(): v.append(obj.next()) assert (v == [3, 7, 9, 15, 20])
self.val = val self.left = left self.right = right class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: if not root: return 0 def get_height(root): if not root: return 0 return 1 + max(get_height(root.left), get_height(root.right)) lh = get_height(root.left) rh = get_height(root.right) rdd = lh + rh ld = self.diameterOfBinaryTree(root.left) rd = self.diameterOfBinaryTree(root.right) return max(rdd, max(ld, rd)) c = Codec() root = c.deserialize("1,2,3,4,5") print(Solution().diameterOfBinaryTree(root))
# Definition for a binary tree node. from problems.lc_297 import Codec class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def str2tree(self, s: str) -> TreeNode: def getnum(str): sign = 1 if str[0] == "-": sign =-1 codec = Codec() node = codec.deserialize("4,2,3,1,6,5") assert Solution().str2tree("4(2(3)(1))(6(5))") == node
while len(lst_even) > 0 or len(lst_odd) > 0: if odd: current_lst = lst_odd next_lst = lst_even else: current_lst = lst_even next_lst = lst_odd element = current_lst.pop(0) if element and element.left: next_lst.append(element.left) if element and element.right: next_lst.append(element.right) if len(current_lst) == 0: res.append(element.val) odd = not odd return res codec = Codec() # str1= "1,2,3,None,5,None,4" # node = codec.deserialize(str1) # print(Solution().rightSideView(node)) str1 = "1,2,3,4" node = codec.deserialize(str1) print(Solution().rightSideView(node)) print("done")
def isValidBST(self, node): return self._isValidBST(node, sys.maxsize, - sys.maxsize -1) def _isValidBST(self, node, l, u): if not node: return True if node.left and not self._checkbound(node.left.value, l, node.value): return False if node.right and not self._checkbound(node.right.value, node.value, u): return False if not self.isValidBST(node.left) or not self.isValidBST(node.right): return False return True def _checkbound(self, n, l, u): if l <= n and n <= u: return True return False codec = Codec() root = codec.deserialize("7,3,10,1,4,8,11") assert(Solution().isValidBST(root) == True) tree = build_binary_tree("2,1,1,1") assert(Solution().isValidBST(tree) == False)
if root and root.left: stack.append(root.left) root.left = None elif root: ans.append(root.val) if k == len(ans): return ans[-1] stack.pop() if root.right: stack.append(root.right) else: stack.pop() codec = Codec() root = codec.deserialize( "41,37,44,24,39,42,48,1,35,38,40,null,43,46,49,0,2,30,36,null,null,null,null,null,null,45,47,null,null,null,null,null,4,29,32,null,null,null,null,null,null,3,9,26,null,31,34,null,null,7,11,25,27,null,null,33,null,6,8,10,16,null,null,null,28,null,null,5,null,null,null,null,null,15,19,null,null,null,null,12,null,18,20,null,13,17,null,null,22,null,14,null,null,21,23" ) assert (Solution().kthSmallest(root, 25) == 24) root = codec.deserialize("1") assert (Solution().kthSmallest(root, 1) == 1) root = codec.deserialize("3,1,4,None,2") assert (Solution().kthSmallest(root, 1) == 1) root = codec.deserialize("5,3,6,2,4,None,None,1") assert (Solution().kthSmallest(root, 3) == 3)
inorder(root.left) node = Node(root.val) node.left = last if head is None: head = node if last is not None: last.right = node last = node inorder(root.right) inorder(root) if last: last.right = head if head: head.left = last return head codec = Codec() root = codec.deserialize("4,2,5,1,3") val = Solution().treeToDoublyList(root) root = codec.deserialize("4,2,5,1,3") val = Solution().treeToDoublyList(root) print("done")
class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: stack = [root] ans = [] while len(stack) > 0: root = stack[-1] if root and root.left: stack.append(root.left) root.left = None elif root: ans.append(root.val) stack.pop() if root.right: stack.append(root.right) else: stack.pop() return ans codec = Codec() root = codec.deserialize("1,None,2,3") assert Solution().inorderTraversal(root) == [1, 3, 2]
return c elif parent is not None: return parent else: return None a = find(root.left, p, root) if a is not None: return a b = find(root.right, p, parent) if b is not None: return b return find(root, p, None) codec = Codec() root = codec.deserialize("6,2,8,0,4,7,9,null,null,3,5") assert Solution().inorderSuccessor(root, TreeNode(2)).val == 3 root = codec.deserialize("5,3,6,1,4,null,null,null,2") assert Solution().inorderSuccessor(root, TreeNode(4)).val == 5 root = codec.deserialize("2,1,3") assert Solution().inorderSuccessor(root, TreeNode(1)).val == 2 root = codec.deserialize("5,3,6,2,4,null,null,1") assert Solution().inorderSuccessor(root, TreeNode(6)) == None