Algorithm 一起穿过两棵树?

Algorithm 一起穿过两棵树?,algorithm,Algorithm,我正在寻找很多面试问题,这些问题需要同时穿越两棵树,我不知道到底该怎么做 e、 g 给定对两个二叉树的根的引用,如何确定 叶元素的顺序是否相等,但必须 当第一个节点违反 规则 您的问题是要了解是否: “通过访问2棵树的所有叶节点创建的序列是相同的” 当发现叶值不匹配时,我们必须立即退出 如果是的话,我提出以下解决方案: insert (root of tree1) in stack1 insert (root of tree2) in stack2 temp1 = (root of tree1

我正在寻找很多面试问题,这些问题需要同时穿越两棵树,我不知道到底该怎么做

e、 g

给定对两个二叉树的根的引用,如何确定 叶元素的顺序是否相等,但必须 当第一个节点违反 规则


您的问题是要了解是否: “通过访问2棵树的所有叶节点创建的序列是相同的”

当发现叶值不匹配时,我们必须立即退出

如果是的话,我提出以下解决方案:

insert (root of tree1) in stack1
insert (root of tree2) in stack2

temp1 = (root of tree1) -> left child
temp2 = (root of tree2) -> left child

while(stack1 and stack2 arent empty) 
{
    found = false
    while(found == false) {
        if (temp1 is leaf node)
            child1 = value of temp1
            found = true
            pop element from stack1
            set temp1 to its right child

        if (temp1 has left child)
            put temp1 in stack1
            set temp1 to its left child
    }

    found = false
    while(found == false) {
        if (temp2 is leaf node)
            child2 = value of temp2
            found = true
            pop element from stack2
            set temp2 to its right child

        if (temp2 has left child)
            put temp2 in stack2
            set temp2 to its left child
    }

    if(child1 != child2) 
        return
}
在伪代码中:

  • 下到每棵树的第一片叶子(比如说最左边的叶子)
  • 比较一下
  • 如果不等于返回错误
  • 走到每棵树的下一片叶子上(凭直觉——向上走,直到你看到一条通向右边孩子的路,然后只带左边的孩子走,直到你到达一片叶子)
  • 如果其中一棵树有一片叶子,但另一棵树返回到根,则返回错误
  • 如果两个树都返回到根,则返回成功
  • 转至步骤2

  • 一种可能的解决办法:

    • 我创建了一个树类,它有一个GetNextLeafNode()方法。它负责返回树的下一个直接叶节点

    • 对于tree类,我保留了一个堆栈来维护遍历的元素

    • 在GetNextLeafNode()方法中,我正在进行迭代树遍历(预排序)

    每当我遇到一个节点(stack.Pop())是leaf,我就返回它。否则,我将向左和向右指针推送到堆栈。最初,根节点被推送。在任何时候,堆栈的状态都是正确的

    下面是C#中的代码:

    现在,我们可以创建两个不同的树,比如t1和t2。 我们可以做如下比较:

            int data1 = t1.GetNextLeafNode().Data;
            int data2 = t2.GetNextLeafNode().Data;
    
            while (data1 == data2)
            {
                //both the leaf nodes are same.
                data1 = t1.GetNextLeafNode().Data;
                data2 = t2.GetNextLeafNode().Data;
            }
    
    一个简单的python解决方案。 虽然这不是空间优化,因为我们存储的叶子可以是O(N+M)。这不是将两棵树一起迭代。 时间复杂度-O(N+M)

    您还可以以类似的方式考虑空间为O(max(N,M))的解决方案

    def binaryTreeLeafs(root1, root2):
    
        # The order in which we see leaves will
        # be maintained as it is inorder traversal.
        def dfs(node, leaves):
            if not node:
                return
            if not node.left and not node.right:
                leaves.append(node.val)
                return
            dfs(node.left, leaves)
            dfs(node.right, leaves)
    
        leaves1 = []
        leaves2 = []
        dfs(root1, leaves1)
        dfs(root2, leaves2)
        return leaves1 == leaves2
    
    
    # O(h1+h2) space
    def binaryTreeLeaves2(root1, root2):
        def isLeaf(node):
            return not node or node.left == node.right == None
        if not root1 and not root2:
            return True
        if (not root1) ^ (not root2):
            return False
        stack1 = [root1]
        stack2 = [root2]
        while stack1 or stack2:
            if (not stack1) ^ (not stack2):
                return False
            tmp1 = stack1.pop()
            while not isLeaf(tmp1):
                if tmp1.right:
                    stack1.append(tmp1.right)
                if tmp1.left:
                    stack1.append(tmp1.left)
                tmp1 = stack1.pop()
            tmp2 = stack2.pop()
            while not isLeaf(tmp2):
                if tmp2.right:
                    stack2.append(tmp2.right)
                if tmp2.left:
                    stack2.append(tmp2.left)
                tmp2 = stack2.pop()
            if ((not tmp1) ^ (not tmp2)) or (tmp1 and tmp2 and tmp1.val != tmp2.val):
                return False        
        return True 
    

    您建议的解决方案听起来不错,步骤4似乎可以通过顺序遍历完成。如果temp1不是叶节点,并且只有右(叶)子节点,您将如何到达那里?
    def binaryTreeLeafs(root1, root2):
    
        # The order in which we see leaves will
        # be maintained as it is inorder traversal.
        def dfs(node, leaves):
            if not node:
                return
            if not node.left and not node.right:
                leaves.append(node.val)
                return
            dfs(node.left, leaves)
            dfs(node.right, leaves)
    
        leaves1 = []
        leaves2 = []
        dfs(root1, leaves1)
        dfs(root2, leaves2)
        return leaves1 == leaves2
    
    
    # O(h1+h2) space
    def binaryTreeLeaves2(root1, root2):
        def isLeaf(node):
            return not node or node.left == node.right == None
        if not root1 and not root2:
            return True
        if (not root1) ^ (not root2):
            return False
        stack1 = [root1]
        stack2 = [root2]
        while stack1 or stack2:
            if (not stack1) ^ (not stack2):
                return False
            tmp1 = stack1.pop()
            while not isLeaf(tmp1):
                if tmp1.right:
                    stack1.append(tmp1.right)
                if tmp1.left:
                    stack1.append(tmp1.left)
                tmp1 = stack1.pop()
            tmp2 = stack2.pop()
            while not isLeaf(tmp2):
                if tmp2.right:
                    stack2.append(tmp2.right)
                if tmp2.left:
                    stack2.append(tmp2.left)
                tmp2 = stack2.pop()
            if ((not tmp1) ^ (not tmp2)) or (tmp1 and tmp2 and tmp1.val != tmp2.val):
                return False        
        return True