:rtype: int """ def dfs(node): if not node: return NO_NEED left = dfs(node.left) right = dfs(node.right) if left == NO_CAMERA or right == NO_CAMERA: self.res += 1 return HAS_CAMERA return NO_NEED if left == HAS_CAMERA or right == HAS_CAMERA else NO_CAMERA if not root: return 0 if dfs(root) == NO_CAMERA: self.res += 1 return self.res if __name__ == '__main__': line = "[0,0,null,0,0]" root = leetcode_utils.stringToTreeNode(line) out = Solution().minCameraCover(root) print out
# 将两个子数组加起来,计算所有节点的距离 if len(leftPathArray) > 0 and len(rightPathArray) > 0: for i in range(len(leftPathArray)): for j in range(len(rightPathArray)): left = leftPathArray[i] right = rightPathArray[j] if left + right <= distance: self.ans += 1 for i in range(len(leftPathArray)): pathArray.append(leftPathArray[i]) for j in range(len(rightPathArray)): pathArray.append(rightPathArray[j]) elif len(leftPathArray) == 0: for i in range(len(rightPathArray)): pathArray.append(rightPathArray[i]) else: for i in range(len(leftPathArray)): pathArray.append(leftPathArray[i]) return pathArray dfs(root) return self.ans if __name__ == '__main__': root = "[1,84,null,80,61]" distance = 2 tree = leetcode_utils.stringToTreeNode(root) print Solution().countPairs(tree, distance)
if t1 and t2: node.val = t1.val + t2.val node.left = self.mergeTrees2(t1.left, t2.left) node.right = self.mergeTrees2(t1.right, t2.right) if t1 and not t2: node.val = t1.val node.left = t1.left node.right = t1.right if not t1 and t2: node.val = t2.val node.left = t2.left node.right = t2.right return node if __name__ == '__main__': line1 = "[1,3,2,5]" t1 = leetcode_utils.stringToTreeNode(line1) line2 = "[2,1,3,null,4,null,7]" t2 = leetcode_utils.stringToTreeNode(line2) # ret = Solution().mergeTrees(t1, t2) # out = leetcode_utils.treeNodeToString(ret) ret2 = Solution().mergeTrees2(t1, t2) out2 = leetcode_utils.treeNodeToString(ret2) # print out print out2
right_value = array[right_index] rightNode = TreeNode(right_value) node.right = rightNode queue.append(rightNode) else: break index += 1 return root class Solution(object): def __init__(self): pass def func(self, root): pass if __name__ == '__main__': treeLine = "[1,2,3,null,null,4,5]" empty = "[]" node = Codec().deserialize(empty) res = Codec().serialize(node) print res # res = Codec().deserialize(treeLine) # print res tree = leetcode_utils.stringToTreeNode(treeLine) mytree = Codec().deserialize(treeLine) # print Codec().serialize(tree) print leetcode_utils.treeNodeToString(mytree)
while q: if q.val in self.vis.keys(): return q q = self.fa[q.val] pass def dfs(self,node): if node.left: self.fa[(node.left.val)] = node self.dfs(node.left) if node.right: self.fa[(node.right.val)] = node self.dfs(node.right) if __name__ == '__main__': line = "[3,5,1,6,2,0,8,null,null,7,4]" root = leetcode_utils.stringToTreeNode(line) line = "[5]" # p = leetcode_utils.stringToInt(line) p_node = leetcode_utils.stringToTreeNode(line) line = "[1]" # q = leetcode_utils.stringToInt(line) q_node = leetcode_utils.stringToTreeNode(line) ret = Solution().lowestCommonAncestor2(root, p_node, q_node) out = leetcode_utils.treeNodeToString(ret) print out