def clone(node, cloned=None): if not node: return None if cloned: cloned.val = node.val else: cloned = Node(node.val) cloned.random = node.random node.random = cloned cloned.left = clone(node.left, cloned.left) cloned.right = clone(node.right, cloned.right) return cloned
def construct(arr, types, S=[], prev=None): node = Node(arr[0]) S.append(node) for n, t in zip(arr[1:], types[1:]): if t == 'N' and S: prev = Node(n) S[-1].left = prev S.append(prev) else: if prev == S[-1].left: S[-1].right = Node(n) prev = S.pop() else: prev = Node(n) S[-1].left = prev return node
def traverse(node, Q=collections.deque([])): Q.append(node) Q.append(None) while Q: current = Q.popleft() if current: if current.left: Q.append(current.left) if current.right: Q.append(current.right) if not Q[0]: Q.append(None) print(current.val, end="\t") else: print() root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) root.left.left.left = Node(8) root.left.left.right = Node(9) root.left.right.left = Node(10) root.left.right.right = Node(11) root.right.left.left = Node(12) root.right.left.right = Node(13)
from src.main.python.algo.medium.tree.model.node import Node def morris(node): print() root = Node(1, right=Node(3), left=Node(2, left=Node(4), right=Node(5) ) )
from src.main.python.algo.medium.tree.model.node import Node def solve(node, S=[], prev=None): if not node: return None S.append(node) while S: if S[-1].right == prev: prev = S.pop() print(prev.val) elif S[-1].left == prev: if S[-1].right: prev = S[-1].right S.append(S[-1].right) elif S[-1].left: prev = S[-1].left S.append(S[-1].left) else: prev = S.pop() print(prev.val) root = Node(1, left=Node(2, left=Node(4), right=Node(5)), right=Node(3, left=Node(6), right=Node(7))) # 1 # 2 3 # 4 5 6 7 solve(root)
from src.main.python.algo.medium.tree.model.node import Node def solve(node): if not node: return 0 r = solve(node.right) l = solve(node.left) node.val, res = r + l, node.val return res + node.val def traverse(node): if not node: return None traverse(node.left) print(node.val, end="\t") traverse(node.right) root = Node(10) root.left = Node(-2) root.right = Node(6) root.left.left = Node(8) root.left.right = Node(-4) root.right.left = Node(7) root.right.right = Node(5) solve(root) traverse(root)
def get_max_gain(node): nonlocal M if node is None: return 0 ls = max(get_max_gain(node.left), 0) rs = max(get_max_gain(node.right), 0) M = max(M, node.val + ls + rs) return node.val + max(ls, rs) get_max_gain(root) return M # root = Node(1) # root.right = Node(3) # root.left = Node(2) # root.left.right = Node(5) # root.left.left = Node(4) # root = Node(1) # root.right = Node(3) # root.left = Node(2) root = Node(-10) root.left = Node(9) root.right = Node(20) root.right.right = Node(7) root.right.left = Node(15) print(solve(root))
node.random = cloned cloned.left = clone(node.left, cloned.left) cloned.right = clone(node.right, cloned.right) return cloned def preorder(node): if not node: return print( str(node.val) + "\t:\t" + str(node)[59:] + "\t:\t" + str(node.random)[59:]) preorder(node.left) preorder(node.right) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) root.random = root.right.left.random root.left.left.random = root.right root.left.right.random = root root.right.left.random = root.left.left root.random = root.left print("Preorder traversal of the original tree:") preorder(root)
if find_any(rx, ly): return rx + 1, ly + 1 elif find_any(lx, ry): return lx + 1, ry + 1 else: return float("-inf"), float("-inf") def find_any(x, y): nonlocal res if x > 0 and y > 0 and x == y: res = True elif x > -1 or y > -1: return True return False search(root, x, y) return res root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.left.right.right = Node(15) root.right.left = Node(6) root.right.right = Node(7) root.right.left.right = Node(8) node1 = root.left.right node2 = root.right.right if isCousins(root, node1.val, node2.val): print("Yes") else:
# https://leetcode.com/problems/diameter-of-binary-tree from src.main.python.algo.medium.tree.model.node import Node def solve(root): def dia(root): if root == None: return 0, -1 ld, lh = dia(root.left) rd, rh = dia(root.right) return max(max(rd, ld), lh + rh + 2), max(lh, rh) + 1 return dia(root)[0] root = Node(1) root.right = Node(3) root.left = Node(2) root.left.right = Node(5) root.left.left = Node(4) print(solve(root))
def search(node): if not node: return float('-inf') if toFind == node.val: collect(node, k_distance, True, True) return k_distance - 1 else: l = search(node.left) r = search(node.right) if r > -1: collect(node, r, True, False) return r - 1 if r - 1 >= 0 else float('-inf') if l > -1: collect(node, l, False, True) return l - 1 if l - 1 >= 0 else float('-inf') def collect(node, k, right, left): if not node: return None if k == 0: return res.append(node.val) if right: collect(node.left, k - 1, True, True) if left: collect(node.right, k - 1, True, True) search(node) return res root = Node(3, Node(5, Node(6), Node(2, Node(7), Node(4))), Node(1, Node(0), Node(8))) # print(solve(root, 5, 2)) root = Node(0, Node(1, Node(3), Node(2))) print(solve(root, 2, 1))
from src.main.python.algo.medium.tree.model.node import Node def solve(node, k): res = 0 def count(node): nonlocal res if not node: return 0 l = count(node.left) r = count(node.right) if l + node.val + r == k: res += 1 return l + node.val + r count(node) return res root = Node(5) root.left = Node(-10) root.right = Node(3) root.left.left = Node(9) root.left.right = Node(8) root.right.left = Node(-4) root.right.right = Node(7) x = 7 ptr = root print(solve(root, x))
from src.main.python.algo.medium.tree.model.node import Node #https://leetcode.com/problems/binary-tree-cameras/ def solve(node): res = 0 def state(node): nonlocal res if not node: return 1 r = state(node.right) l = state(node.left) if l == -1 or r == -1: res += 1 return 0 elif l == 0 or r == 0: return 1 else: return -1 s = state(node) return res if s != -1 else res + 1 root = Node(1, Node(4, Node(3), Node(5))) print(solve(root))