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/deepest_left.py at master · vspriya05/algorithms Expand file tree/ Copy pathdeepest_left.pyMore file actions 47 lines (39 loc) · 975 Bytes / Copy pathdeepest_left.pyFile metadata and controls47 lines (39 loc) · 975 Bytes 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 # Given a binary tree, find the deepest node # that is the left child of its parent node. # Example: # 1 # / \ # 2 3 # / \ \ # 4 5 6 # \ # 7 # should return 4. class Node: def __init__(self, val = None): self.left = None self.right = None self.val = val class DeepestLeft: def __init__(self): self.depth = 0 self.Node = None def find_deepest_left(root, is_left, depth, res): if not root: return if is_left and depth > res.depth: res.depth = depth res.Node = root find_deepest_left(root.left, True, depth + 1, res) find_deepest_left(root.right, False, depth + 1, res) root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(6) root.right.right.right = Node(7) res = DeepestLeft() find_deepest_left(root, True, 1, res) if res.Node: print(res.Node.val) |
| 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. |