def lca(root, v1, v2): while root: if v1v2: root=root.left elif v1>root.info=low: return False if check_binary_search_tree_(root.left,min(root.data,low),high) and check_binary_search_tree_(root.right,low,max(high,root.data)): return True return False class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break def isleaf(node): if not node: return False if not node.left and not node.right: return True return False def leftsum(root): res=0 if root: if isleaf(root.left): res+=root.left.info else: res+=leftsum(root.left) res+=leftsum(root.right) return res tree=BinarySearchTree() n=int(input()) l=list(map(int,input().split())) for i in l: tree.create(i) print(leftsum(tree.root)) class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break def isleaf(node): if not node: return False if not node.left and not node.right: return True return False def rightsum(root): res=0 if root: if isleaf(root.right): res+=root.right.info else: res+=rightsum(root.right) res+=rightsum(root.left) return res tree=BinarySearchTree() n=int(input()) l=list(map(int,input().split())) for i in l: tree.create(i) print(rightsum(tree.root)) class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break def hasPath(root, arr, x): if (not root): return False arr.append(root.info) if (root.info == x): return True if (hasPath(root.left, arr, x) or hasPath(root.right, arr, x)): return True arr.pop(-1) return False def printPath(root, x): arr = [] hasPath(root,arr,x) print(*arr) tree=BinarySearchTree() n=int(input()) l=list(map(int,input().split())) x=int(input()) for i in l: tree.create(i) (printPath(tree.root,x)) class Node: def __init__(self, info): self.info = info self.left = None self.right = None self.level = None def __str__(self): return str(self.info) class BinarySearchTree: def __init__(self): self.root = None def create(self, val): if self.root == None: self.root = Node(val) else: current = self.root while True: if val < current.info: if current.left: current = current.left else: current.left = Node(val) break elif val > current.info: if current.right: current = current.right else: current.right = Node(val) break else: break def height(node): if not node: return 0 return max(height(node.left),height(node.right))+1 def diameter(root): if not root: return 0 lheight=height(root.left) rheight=height(root.right) ldia=diameter(root.left) rdia=diameter(root.right) return max(lheight+rheight+1,max(ldia,rdia)) tree=BinarySearchTree() n=int(input()) l=list(map(int,input().split())) for i in l: tree.create(i) print(diameter(tree.root))