<jupyter_start><jupyter_text>Lowest Common Manager[link](https://www.algoexpert.io/questions/Lowest%20Common%20Manager)  My Solution<jupyter_code>def getLowestCommonManager(topManager, reportOne, reportTwo):
    # Write your code here.
    return getLCMHelper(topManager, reportOne, reportTwo)
    
def getLCMHelper(node, reportOne, reportTwo):
    count = 0
    if node == reportOne or node == reportTwo:
        count += 1
    for n in node.directReports:
        c = getLCMHelper(n, reportOne, reportTwo)
        if type(c) == int:
            count += c
        else:
            return c
    if count == 2:
        return node
    return count
    
    
# This is an input class. Do not edit.
class OrgChart:
    def __init__(self, name):
        self.name = name
        self.directReports = []

def getLowestCommonManager(topManager, reportOne, reportTwo):
    # Write your code here.
    return getLCMHelper(topManager, reportOne, reportTwo)[1]
    
def getLCMHelper(node, reportOne, reportTwo):
    count = 0
    if node == reportOne or node == reportTwo:
        count += 1
    for n in node.directReports:
        c, returnNode = getLCMHelper(n, reportOne, reportTwo)
        if returnNode is not None:
            return c, returnNode
        count += c
    if count == 2:
        return count, node
    return count, None
    
# This is an input class. Do not edit.
class OrgChart:
    def __init__(self, name):
        self.name = name
        self.directReports = []<jupyter_output><empty_output><jupyter_text>Expert Solution<jupyter_code># O(n) time | O(d) space - where n is the number of people
# in the org and d is the depth (height) of the org chart
def getLowestCommonManager(topManager, reportOne, reportTwo):
    return getOrgInfo(topManager, reportOne, reportTwo).lowestCommonManager

def getOrgInfo(manager, reportOne, reportTwo):
    numImportantReports = 0
    for directReport in manager.directReports:
        orgInfo = getOrgInfo(directReport, reportOne, reportTwo)
        if orgInfo.lowestCommonManager is not None:
            return orgInfo
        numImportantReports += orgInfo.numImportantReports
    if manager == reportOne or manager == reportTwo:
        numImportantReports += 1
    lowestCommonManager = manager if numImportantReports == 2 else None
    return OrgInfo(lowestCommonManager, numImportantReports)

class OrgInfo:
    def __init__(self, lowestCommonManager, numImportantReports):
        self.lowestCommonManager = lowestCommonManager
        self.numImportantReports = numImportantReports


# This is an input class. Do not edit.
class OrgChart:
    def __init__(self, name):
        self.name = name
        self.directReports = []<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08054431776388378, 'fraction_numerical': 0.0051489518205222505, 'mean_word_length': 3.1149773071104385, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '378730', 'license': 'MIT', 'n_tokens_mistral': 828, 'n_tokens_neox': 812, 'n_words': 241, 'path': 'algoExpert/lowest_common_manager/solution.ipynb', 'repo_name': 'maple1eaf/learning_algorithm', 'size': '2719'}