# self.right = None class Solution: def longestUnivaluePath(self, root): """ :type root: TreeNode :rtype: int """ def get_count(cur): if not cur: return 0 left, right = get_count(cur.left), get_count(cur.right) left = (left + 1) if cur.left and cur.left.val == cur.val else 0 right = (right + 1) if cur.right and cur.right.val == cur.val else 0 self.long = max(self.long, left + right) return max(left, right) self.long = 0 get_count(root) return self.long from graph import leetcode_tree t = leetcode_tree([2, 2, 3, 2, 2, 4, 5, 2, 3, 2]) # t = leetcode_tree([1,1]) # t = leetcode_tree([5,4,5,1,1,5]) s = Solution() print(s.longestUnivaluePath(t))
""" :type root: TreeNode :rtype: TreeNode """ def max_depth(cur): if not cur: return 0, 0 left_node, left_depth = max_depth(cur.left) right_node, right_depth = max_depth(cur.right) if left_depth == right_depth: return cur, 1+left_depth elif left_depth > right_depth: return left_node, 1+left_depth return right_node, 1+right_depth if not root: return root node, depth = max_depth(root) return node from graph import leetcode_tree g = [3,5,1,6,2,0,8,None,None,7,4] g = [1,2,3,4,5] t = leetcode_tree(g) s = Solution() ans = s.subtreeWithAllDeepest(t) print(ans.val) if ans else print("none tyupe")
class Solution: def isCousins(self, root: 'TreeNode', x: 'int', y: 'int') -> 'bool': self.xp = None self.xl = None self.yp, self.yl = None, None def build(cur, k, parent): if cur: if cur.val == x: self.xp, self.xl = parent, k if cur.val == y: self.yp, self.yl = parent, k build(cur.left, k + 1, cur) build(cur.right, k + 1, cur) build(root, 0, None) return ((self.xl == self.yl) and (self.xp != self.yp)) from graph import leetcode_tree t = leetcode_tree([1, 2, 3, 4, 5, 6, 7]) print(Solution().isCousins(t, 5, 3))
build(cur.right)) return build(root) def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ def build(cur, i, l): if i >= l or cur[i] == '#': return None, i + 1 node = TreeNode(cur[i]) node.left, li = build(cur, i + 1, l) node.right, ri = build(cur, li, l) return node, ri d = data.split(",") l = len(d) return build(d, 0, l)[0] # Your Codec object will be instantiated and called as such: from graph import leetcode_tree, TreeNode, inorder t = leetcode_tree([1, 2, 3, 4]) t = leetcode_tree([]) codec = Codec() # print(codec.serialize(t)) nr = codec.deserialize(codec.serialize(t)) print(inorder(nr))
seen = defaultdict(list) cur = [(root, 0, 0)] while cur: nxt = [] for (node, x, y) in cur: seen[x].append((-y, node.val)) if node.left: nxt.append((node.left, x - 1, y - 1)) if node.right: nxt.append((node.right, x + 1, y - 1)) cur = nxt # print(seen) for val in seen.values(): val.sort() result = [] for key, val in seen.items(): valup = [v for _, v in val] result.append((key, valup)) result.sort() return [val[1] for val in result] from graph import leetcode_tree t = leetcode_tree( [1, 2, 3, 4, 5, 6, 7, None, None, 8, 9, None, None, 10, None]) t = leetcode_tree( [0, 2, 1, 3, None, None, None, 4, 5, None, 7, 6, None, 10, 8, 11, 9]) t = leetcode_tree([1, 2, 3, 4, 5, 6, 7]) t = leetcode_tree([3, 9, 20, None, None, 15, 7]) print(Solution().verticalTraversal(t))
""" :type root: TreeNode :rtype: int """ self.levels = {} def build(cur, n, l): if not cur: return if l not in self.levels: self.levels[l] = [float('inf'), -float('inf')] self.levels[l] = [ min(self.levels[l][0], n), max(self.levels[l][1], n) ] build(cur.left, 2 * n + 1, l + 1) build(cur.right, 2 * n + 2, l + 1) build(root, 0, 0) ans = 0 for (l, r) in self.levels.values(): ans = max(ans, 1 + r - l) return ans from graph import leetcode_tree t = leetcode_tree([1, 2, 3, 4, None, None, 7, 8, None, None, 9]) t = leetcode_tree([1]) print(Solution().widthOfBinaryTree(t))
cur_node = target.val while cur_node != root_node: cur_parent = parents[cur_node] distances[cur_parent] = distances[cur_node] + 1 cur_node = cur_parent def get_distances(cur): if cur: if cur.left and distances[cur.left.val] == -1: distances[cur.left.val] = distances[cur.val] + 1 if cur.right and distances[cur.right.val] == -1: distances[cur.right.val] = distances[cur.val] + 1 get_distances(cur.left) get_distances(cur.right) get_distances(root) # print(distances) ans = [] for val, dist in distances.items(): if dist == K: ans.append(val) return ans from graph import leetcode_tree, TreeNode t1 = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4] t2 = [] t = leetcode_tree(t2) s = Solution() print(s.distanceK(t, TreeNode(5), 10))
return root if not cur.right: cur.val = cur.left.val cur.right = cur.left.right cur.left = cur.left.left return root curright = cur.right curparent = cur while curright.left: curparent = curright curright = curright.left if curparent == cur: cur.right = None cur.val = curright.val if not curright.right: # print("removing", curright, curparent) remove_leav(curright, curparent) else: curright.val = curright.right.val curright.left = curright.right.left curright.right = curright.right.right return root from graph import leetcode_tree, inorder t = leetcode_tree([10, 5, 15, 3, 6, 12, 20]) t = leetcode_tree([2, None, 3]) Solution().deleteNode(t, 2) print(inorder(t))
from graph import leetcode_tree class Solution: def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ cur = root stk = [] ans = [] while cur or stk: # print(cur, stk) while cur: stk.append(cur) cur = cur.left while stk: top = stk.pop() ans.append(top) if top.right: cur = top.right break return ans s = Solution() t = leetcode_tree([]) # print(t.val) print(s.inorderTraversal(t))
# self.left = None # self.right = None class Solution: def postorderTraversal(self, head): """ :type root: TreeNode :rtype: List[int] """ cur = head stk = [] ans = [] while stk or cur: # print(stk, cur) while cur: stk.append(cur) cur = cur.left ans.append(stk.pop()) while stk: print(stk) top = stk[-1] if top.right: cur = top.right else: ans.append(stk.pop()) return ans s = Solution() t = leetcode_tree([1,2,3,4,5]) print(s.postorderTraversal(t))
from collections import defaultdict class Solution(object): def pathSum(self, root, sum): """ :type root: TreeNode :type sum: int :rtype: int """ def build(cur, seen): if not cur: return # print(cur.val, seen) if cur.val in seen: self.count += seen[cur.val] if cur.val == sum: self.count += 1 cursum = sum - cur.val newseen = defaultdict(int) for key, val in seen.items(): newseen[key - cur.val] = val newseen[cursum] += 1 build(cur.left, newseen) build(cur.right, newseen) self.count = 0 build(root, {}) return self.count from graph import leetcode_tree null = None t = leetcode_tree([10,5,-3,3,2,null,11,3,-2,null,1]) print(Solution().pathSum(t, 17))
from collections import defaultdict class Solution: def findDuplicateSubtrees(self, root): """ :type root: TreeNode :rtype: List[TreeNode] """ def build(cur, seen): if not cur: return '#' key = str(cur.val) + build(cur.left, seen) + build(cur.right, seen) seen[key].append(cur) return key seen = defaultdict(list) build(root, seen) return [val[0] for val in seen.values() if len(val) > 1] from graph import leetcode_tree t = leetcode_tree([0,0,0,0,None,None,0,None,None,None,0]) Solution().findDuplicateSubtrees(t)