二叉搜索树的最近公共祖先在长输入案例中失败
问题
给定一个二叉搜索树(BST),需要找到这棵树中两个指定节点的最近公共祖先(LCA)。
根据维基百科对LCA的定义: “最近公共祖先是指在树T中,节点p和q的最低节点,这个节点同时是p和q的后代(允许一个节点是它自己的后代)。”
逻辑
可以用两个队列来记录路径(根据二叉搜索树的逻辑遍历),然后同时遍历这两个队列,一旦发现不一致的地方,就返回之前的节点。我知道其实可以不使用队列,通过BST的逻辑来实现这个功能,最终我会进行优化。但我只是想知道,为什么现在这个方法不奏效。
代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import collections
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
queueForp = collections.deque()
queueForq = collections.deque()
phead = root
qhead = root
while phead != None:
queueForp.append(phead)
if phead == p:
phead = None
else:
if phead.val < p.val:
phead = phead.right
else:
phead = phead.left
while qhead!= None:
queueForq.append(qhead)
if qhead == q:
qhead = None
else:
if qhead.val < q.val:
qhead = qhead.right
if qhead.val > q.val:
qhead = qhead.left
print(queueForp)
print(queueForq)
prev = None
while len(queueForp)!= 0 and len(queueForq) != 0:
currp = queueForp.popleft()
currq = queueForq.popleft()
if currp.val == currq.val:
prev = currp
else:
return prev
return prev
问题
在处理一个非常大的输入(大约1万)时出现了错误。
p = 5893
q = 3379
输出:41 期望输出:5734 另外,你可以尝试把这段代码放到leetcode上,看看测试用例。 我哪里做错了?
1 个回答
0
在编程中,有时候我们会遇到一些问题,像是代码运行不正常或者出现错误信息。这些问题可能会让我们感到困惑,不知道该怎么解决。为了帮助大家更好地理解这些问题,我们可以通过一些简单的例子和解释来理清思路。
首先,了解错误信息是非常重要的。错误信息通常会告诉我们出错的地方和原因。比如,如果你看到“找不到变量”的提示,那就说明你在代码中使用了一个没有定义的变量。这个时候,你需要检查一下你的代码,看看是不是写错了变量名,或者是不是忘记先定义这个变量。
另外,调试代码也是一个很重要的技能。调试就是逐步检查代码,找出问题所在。你可以通过在代码中添加一些打印语句,来查看变量的值,或者代码执行到哪一步了。这样可以帮助你更清楚地了解代码的运行情况,从而找到问题。
总之,遇到问题时不要慌张,仔细阅读错误信息,逐步调试代码,通常都能找到解决办法。编程是一个不断学习和实践的过程,遇到困难是很正常的,重要的是要保持耐心和好奇心。
// Recursive Java program to print lca of two nodes
// A binary tree node
class Node {
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
class BinaryTree {
Node root;
/* Function to find LCA of n1 and n2. The function
assumes that both n1 and n2 are present in BST */
Node lca(Node node, int n1, int n2)
{
if (node == null)
return null;
// If both n1 and n2 are smaller than root, then LCA
// lies in left
if (node.data > n1 && node.data > n2)
return lca(node.left, n1, n2);
// If both n1 and n2 are greater than root, then LCA
// lies in right
if (node.data < n1 && node.data < n2)
return lca(node.right, n1, n2);
return node;
}
/* Driver code */
public static void main(String args[])
{
// Let us construct the BST shown in the above
// figure
BinaryTree tree = new BinaryTree();
tree.root = new Node(20);
tree.root.left = new Node(8);
tree.root.right = new Node(22);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(12);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(14);
// Function calls
int n1 = 10, n2 = 14;
Node t = tree.lca(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2
+ " is " + t.data);
n1 = 14;
n2 = 8;
t = tree.lca(tree.root, n1, n2);
System.out.println("LCA of " + n1 + " and " + n2
+ " is " + t.data);
n1 = 10;
n2 = 22;
t = tree.lca(tree.root, n1, n2);
}
}