:type root2: TreeNode :rtype: TreeNode """ if root1 is None and root2 is None: return None if root1 is None: return root2 if root2 is None: return root1 sum = root1.val + root2.val root = TreeNode(sum) print("Start Calculating left for p = {} & q {}: ".format( root1.val, root2.val)) root.left = self.mergeTrees(root1.left, root2.left) print("Done Calculating left done for p = {} & q {}: ".format( root1.val, root2.val)) print("Start Calculating right for p = {} & q {}: ".format( root1.val, root2.val)) root.right = self.mergeTrees(root1.right, root2.right) print(" Done Calculating right for p = {} & q {}: ".format( root1.val, root2.val)) return root s = Solution() root1 = deserialize('[1,3,2,5]') root2 = deserialize('[2,1,3,null,4,null,7]') drawtree(s.mergeTrees(root1, root2))
def rightSideView(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ result_list = [] #Final result list of list is saved here if root is None: return [] queue = [] # each node's children are saved here queue.append(root) while ( len(queue) > 0 ): # For each root's child, make it as root and save their left & right child next_level_q = [] # Store children of each node at current level for element in queue: if element.right is not None: next_level_q.append(element.right) if element.left is not None: next_level_q.append(element.left) result_list.append(queue[0].val) queue = next_level_q #Updating the queue with next level children to make them current nodes return result_list s = Solution() root = deserialize('[3,9,20,null,null,15,7]') print(s.rightSideView(root))
""" # Definition for a Node. class Node(object): def __init__(self, val=None, children=None): self.val = val self.children = children """ from python.leetcode_tree_helper import deserialize class Solution(object): def preorder(self, root): """ :type root: Node :rtype: List[int] """ s=Solution() root=deserialize('[1,null,3,2,4,null,5,6]') print(s.preorder(root))
# self.left = left # self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def isSameTree(self, p, q): """ :type p: TreeNode :type q: TreeNode :rtype: bool """ if p is None or q is None: if p is None and q is None: return True else: return False if p.val != q.val: return False return self.isSameTree(p.left, q.left) and self.isSameTree( p.right, q.right) s = Solution() p = deserialize('[1,2]') q = deserialize('[1,null,2]') print(s.isSameTree(p, q))
def inOrderTraverse(self, root, result): if root is None: return self.inOrderTraverse(root.left, result) result.append(root.val) self.inOrderTraverse(root.right, result) def inOrderStack(self, root): if root is None: return [] stack = [] prev_el = None current = root while len(stack) > 0 or current is not None: if current is not None: stack.append(current) current = current.left else: temp_node = stack.pop() current = temp_node.right if prev_el and temp_node.val < prev_el: return False prev_el = temp_node.val return True s = Solution() root = deserialize('2,1,3]') print(s.isValidBST(root))
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def invertTree(self, root): """ :type root: TreeNode :rtype: TreeNode """ if root: root.left, root.right = self.invertTree( root.right), self.invertTree(root.left) return root s = Solution() root = deserialize('[4,2,7,1,3,6,9]') print(s.invertTree(root))
:type root: TreeNode :rtype: List[List[int]] """ if root is None: return [] level_dict = {} level_dict[0] = root.val queue = [] # each node's children are saved here queue.append((root, 0)) while ( len(queue) > 0 ): # For each root's child, make it as root and save their left & right child next_level_q = [] # Store children of each node at current level for tup in queue: element = tup[0] pos = tup[1] if pos not in level_dict: level_dict[pos] = element.val if element.left is not None: next_level_q.append((element.left, pos - 1)) if element.right is not None: next_level_q.append((element.right, pos + 1)) queue = next_level_q #Updating the queue with next level children to make them current nodes return list(level_dict.values()) s = Solution() root = deserialize('[1,2,3,4,5,6,7]') print(s.topSideView(root))
class Solution(object): def leftSideView(self, root): """ :type root: TreeNode :rtype: List[List[int]] """ result_list = [] queue = [] if root is None: return [] queue.append(root) while len(queue) > 0: child_q = [] for child in queue: if child.left is not None: child_q.append(child.left) if child.right is not None: child_q.append(child.right) result_list.append(queue[0]) queue = child_q return result_list s = Solution() root = deserialize('[1,2,3,null,5,null,4]') print(s.leftSideView(root))
# self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def rangeSumBST(self, root, low, high): """ :type root: TreeNode :type low: int :type high: int :rtype: int """ if root is None: return 0 sum = 0 if root.val >= low and root.val <= high: sum += root.val if root.val < high: sum += self.rangeSumBST(root.right, low, high) if root.val > low: sum += self.rangeSumBST(root.left, low, high) return sum s = Solution() root = deserialize('[10,5,15,3,7,null,18]') print(s.rangeSumBST(root, low=7, high=15))
# def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def searchBST(self, root, val): """ :type root: TreeNode :type val: int :rtype: TreeNode """ if root is None: return None if root.val == val: return root if root.val > val: return self.searchBST(root.left, val) else: return self.searchBST(root.right, val) s = Solution() root = deserialize('[4,2,7,1,3]') print(s.searchBST(root, val=2))
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from python.leetcode_tree_helper import deserialize, TreeNode, drawtree class Solution(object): def hasPathSum(self, root, targetSum): """ :type root: TreeNode :type targetSum: int :rtype: bool """ if root is None: return False if root.left is None and root.right is None and root.val==targetSum: return True targetSum -=root.val return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum) s=Solution() root = deserialize('[5,4,8,11,null,13,4,7,2,null,null,null,1]') # drawtree(root) print(s.hasPathSum(root, targetSum = 22))
# self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def kthSmallest(self, root, k): """ :type root: TreeNode :type k: int :rtype: int """ if root is None: return None stack=[] counter=1 current=root while len(stack)>0 or current is not None: if current is not None: stack.append(current) current=current.left else: temp_node=stack.pop() current=temp_node.right if counter==k: return temp_node.val counter+=1 return None s=Solution() root = deserialize('[3,1,4,null,2]') print(s.kthSmallest(root,k=1))
return result_list def inOrderRecurs(self, root, result_list): if root is None: return self.inOrderRecurs(root.left, result_list) result_list.append(root.val) self.inOrderRecurs(root.right, result_list) def inOrderStack(self, root): if root is None: return [] stack = [] current = root result = [] while stack or current is not None: if current: stack.append(current) current = current.left else: temp_node = stack.pop() result.append(temp_node.val) current = temp_node.right return result s = Solution() root = deserialize('[1,null,2,3]') print(s.inorderTraversal(root))
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from python.leetcode_tree_helper import deserialize, TreeNode class Solution(object): def minDepth(self, root): """ :type root: TreeNode :rtype: int """ if root is None: return 0 leftNode=self.minDepth(root.left) rightNode=self.minDepth(root.right) if root.left is None and root.right is not None: return 1+rightNode if root.right is None and root.left is not None: return 1+leftNode return 1+min(leftNode, rightNode) s=Solution() root = deserialize('[2,null,3,null,4,null,5,null,6]') print(s.minDepth(root))
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right from python.leetcode_tree_helper import deserialize class Solution(object): def increasingBST(self, root): """ :type root: TreeNode :rtype: TreeNode """ s = Solution() root = deserialize('[5,3,6,2,4,null,8,1,null,null,null,7,9]') print(s.increasingBST(root))