def tag(node, depth): if node != None: node.depth = depth if depth > maxDepth[0]: maxDepth[0] = depth tag(node.left, depth + 1) tag(node.right, depth + 1) tag(root, 1) queue = [root] output = [] for _ in range(maxDepth[0]): output.append([]) while queue != []: node = queue.pop(0) depth = node.depth if node.left != None: queue.append(node.left) if node.right != None: queue.append(node.right) if depth % 2 != 0: output[depth - 1].append(node.val) else: output[depth - 1].insert(0, node.val) return output root = Binary_Tree([1, 2, 3, 4, 5, 6, 7]).root zigzagLevelOrder(root)
Output: 4 / \ 7 2 / \ / \ 9 6 3 1 """ def invertTree(root): if root != None: node = root.left root.left = root.right root.right = node invertTree(root.left) invertTree(root.right) return root root = Binary_Tree([4, 2, 7, 1, 3, 6, 9]).root print(root.val) print(root.left.val, root.right.val) print(root.left.left.val, root.left.right.val, root.right.left.val, root.right.right.val) invertTree(root) print(root.val) print(root.left.val, root.right.val) print(root.left.left.val, root.left.right.val, root.right.left.val, root.right.right.val)
def isValidBSTInorderTraverse(root): if root == None: return True a = [] def inorderTraverse(root): if root.left != None: inorderTraverse(root.left) a.append(root.val) if root.right != None: inorderTraverse(root.right) inorderTraverse(root) print(a) for i in range(0, len(a) - 1): if a[i] >= a[i + 1]: return False return True tree = Binary_Tree([3, 1, 5, 0, 2, 4, 6, None, None, None, 3]).root # tree = Binary_Tree([2,1,3]).root tree.left.left.left = None tree.left.left.right = None tree.left.right.left = None print(isValidBST(tree)) print(isValidBSTInorderTraverse(tree))
from _binary_Tree import Binary_Tree, TreeNode tree = Binary_Tree([1, None, 2]).root tree.left = None tree.right.left = TreeNode(3) def inorderTraversal(root): if root == None: return ans = [] def go(root): if root.left != None: go(root.left) ans.append(root.val) if root.right != None: go(root.right) go(root) return ans print(inorderTraversal(tree))
from _binary_Tree import TreeNode,Binary_Tree def maxDepth(root): if root == None: return 0 def depth(node,d,mxd): # print('val',node.val) # print('d',d) # print('mxd',mxd) if node.left != None: mxd = depth(node.left,d+1,max(d+1,mxd)) if node.right != None: mxd = depth(node.right,d+1,max(d+1,mxd)) return mxd mxd = depth(root,1,1) return mxd def maxDepth_DP(root): if root == None: return 0 return 1 + max(maxDepth_DP(root.right),maxDepth_DP(root.left)) t = Binary_Tree([3,9,20,None,None,15,7]).root t.left.left = None t.left.right = None print('Max Depth = ',maxDepth_DP(t)) print('Max Depth = ',maxDepth(t))
Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its minimum depth = 2. """ from _binary_Tree import Binary_Tree, TreeNode class Solution: def minDepth(self, root): if root == None: return 0 if root.left == None or root.right == None: return self.minDepth(root.right) + self.minDepth(root.left) + 1 return 1 + min(self.minDepth(root.right), self.minDepth(root.left)) root = Binary_Tree([3, 9]).root root.left.left = None root.left.right = None sol = Solution() print(sol.minDepth(root))