GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/tree/max_height.py at master · vspriya05/algorithms Expand file tree/ Copy pathmax_height.pyMore file actions 54 lines (46 loc) · 1.14 KB / Copy pathmax_height.pyFile metadata and controls54 lines (46 loc) · 1.14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 """ Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. """ class Node(): def __init__(self, val = 0): self.val = val self.left = None self.right = None # def max_height(root): # if not root: # return 0 # return max(maxDepth(root.left), maxDepth(root.right)) + 1 # iterative def max_height(root): if not root: return 0 height = 0 queue = [root] while queue: height += 1 level = [] while queue: node = queue.pop(0) if node.left: level.append(node.left) if node.right: level.append(node.right) queue = level return height def print_tree(root): if root: print(root.val) print_tree(root.left) print_tree(root.right) tree = Node(10) tree.left = Node(12) tree.right = Node(15) tree.left.left = Node(25) tree.left.left.right = Node(100) tree.left.right = Node(30) tree.right.left = Node(36) height = max_height(tree) print_tree(tree) print("height:", height) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |