# Definition for a binary tree node. class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def findTarget(self, root: TreeNode, k: int) -> bool: mystack = [] def find(root: TreeNode, k: int, mystack: list): if root is None: return False if (k - root.val) in mystack: return True mystack.append(root.val) return find(root.left, k, mystack) or find(root.right, k, mystack) return find(root, k, mystack) root = [5, 3, 6, 2, 4, "null", 7] k = 9 # root = [5, 3, 6, 2, 4, "null", 7]; k = 28 t = MyTree() root1 = t.maketree(root) s = Solution() print(s.findTarget(root1, k))
if t1: res = t1 elif t2: res = t2 else: return [] traversal(t1, t2) return res def mergeTrees2(self, t1: TreeNode, t2: TreeNode) -> TreeNode: if t1 and t2: node = TreeNode(t1.val + t2.val) node.left = self.mergeTrees(t1.left, t2.left) node.right = self.mergeTrees(t1.right, t2.right) return node return t1 or t2 tt1 = [1, 3, 2, 5] tt2 = [2, 1, 3, "null", 4, "null", 7] tt1 = [] tt2 = [1] t = MyTree() t1 = t.maketree(tt1) t2 = t.maketree(tt2) s = Solution() res = s.mergeTrees(t1, t2) print(t.print_tree(res))
self.right = right class Solution: def minDepth(self, root: TreeNode) -> int: if not root: return 0 mindepth = 10 ** 5 def traversal(node: TreeNode, depth, mindepth): if node: depth += 1 if not node.left and not node.right: if mindepth > depth: mindepth = depth return mindepth if node.left: mindepth = min(traversal(node.left, depth, mindepth), mindepth) if node.right: mindepth = min(traversal(node.right, depth, mindepth), mindepth) return mindepth return traversal(root, 0, mindepth) root = [3,9,20,"null","null",15,7] root = [2,"null",3,"null",4,"null",5,"null",6] t = MyTree() rr = t.maketree(root) s = Solution() print(s.minDepth(rr))
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def searchBST(self, root: TreeNode, val: int) -> TreeNode: def traverse(root: TreeNode, val: int): if not root: return None if root.val == val: return root elif root.val > val: return traverse(root.left, val) else: return traverse(root.right, val) return traverse(root, val) root = [4, 2, 7, 1, 3] val = 2 root = [4, 2, 7, 1, 3] val = 5 m = MyTree() mt = m.maketree(root) s = Solution() print(s.searchBST(mt, val))
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: if p is None and q is None: return True if not p or not q: return False if p.val != q.val: return False return self.isSameTree(p.left, q.left) and \ self.isSameTree(p.right, q.right) p = [1, 2, 3] q = [1, 2, 3] p = [1, 2] q = [1, "null", 2] p = [1, 2, 1] q = [1, 1, 2] t = MyTree() pp = t.maketree(p) qq = t.maketree(q) s = Solution() print(s.isSameTree(pp, qq))
from iv.Binarytree.mytree import MyTree arr = [100, 98, 102, 96, 99, "null", "null", "null", 97] m = MyTree() root = m.maketree(arr) res = m.print_tree(root) print(res)
# print(res[i].val, res[i+1].val) if res[i].val > res[i + 1].val: node1 = res[i] notfound = False else: if res[i].val < res[i - 1].val: ind.append(i) # print(ind) node2 = res[max(ind)] # print(node1.val, node2.val) node1.val, node2.val = node2.val, node1.val t = TreeNode(3) t.left = TreeNode(1) t.right = TreeNode(4) t.right.left = TreeNode(2) t = TreeNode(1) t.left = TreeNode(3) t.left.right = TreeNode(2) obj = MyTree() root = [1, 3, "null", "null", 2] # root = [3,1,4,"null","null",2] t = obj.maketree(root) s = Solution() s.recoverTree(t) print(obj.print_tree(t))
traverse(root1) res1 = [i for i in res] res = [] traverse(root2) res2 = [i for i in res] # print(res1) # print(res2) if res1 == res2: return True else: return False root1 = [3, 5, 1, 6, 2, 9, 8, "null", "null", 7, 4] root2 = [ 3, 5, 1, 6, 7, 4, 2, "null", "null", "null", "null", "null", "null", 9, 8 ] root1 = [1] root2 = [2] root1 = [1, 2] root2 = [2, 2] root1 = [1, 2, 3] root2 = [1, 3, 2] t = MyTree() rr1 = t.maketree(root1) rr2 = t.maketree(root2) s = Solution() print(s.leafSimilar(rr1, rr2)) print(s.leafSimilar2(rr1, rr2))
# Definition for a binary tree node. class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def pruneTree(self, root: TreeNode) -> TreeNode: def isvalid(node: TreeNode): if not node: return False a1 = isvalid(node.left) a2 = isvalid(node.right) if not a1: node.left = None if not a2: node.right = None return node.val == 1 or a1 or a2 return root if isvalid(root) else None t = MyTree() mytt = [1, "null", 0, 0, 1] root = t.maketree(mytt) s = Solution() print(s.pruneTree(root)) res = t.print_tree(s.pruneTree(root)) print(res)
self.right = right class Solution: def minDiffInBST(self, root: TreeNode) -> int: mylist = [] def traverse(root): if root: if root.left: traverse(root.left) mylist.append(root.val) if root.right: traverse(root.right) traverse(root) min_dist = max(mylist) for i in range(len(mylist) - 1): if abs(mylist[i] - mylist[i + 1]) < min_dist: min_dist = abs(mylist[i] - mylist[i + 1]) return min_dist root = [4, 2, 6, 1, 3] root = [] root = [1, 0, 48, "null", "null", 12, 49] mt = MyTree() root_bst = mt.maketree(root) s = Solution() print(s.minDiffInBST(root_bst))