要找到一个二叉树中两个节点的最低公共祖先(Lowest Common Ancestor, LCA),需要考虑以下几点:
下面是用 Go 实现二叉树中两个节点的最低公共祖先(LCA)可以采用递归的方法,这里假设已经定义了二叉树节点的结构体:
package main
import "fmt"
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func lowestCommonAncestor(root, A, B *TreeNode) *TreeNode {
// Base case: if root is nil or equal to A or B, return root
if root == nil || root == A || root == B {
return root
}
// Recursively search left and right subtrees for LCA
leftLCA := lowestCommonAncestor(root.Left, A, B)
rightLCA := lowestCommonAncestor(root.Right, A, B)
// If both leftLCA and rightLCA are non-nil, then root is the LCA
if leftLCA != nil && rightLCA != nil {
return root
}
// Otherwise, LCA is either in left subtree or right subtree
if leftLCA != nil {
return leftLCA
}
return rightLCA
}
func main() {
// Example usage:
// Construct a binary tree
// 3
// / \
// 5 1
// / \ / \
// 6 2 0 8
// / \
// 7 4
root := &TreeNode{Val: 3}
root.Left = &TreeNode{Val: 5}
root.Right = &TreeNode{Val: 1}
root.Left.Left = &TreeNode{Val: 6}
root.Left.Right = &TreeNode{Val: 2}
root.Left.Right.Left = &TreeNode{Val: 7}
root.Left.Right.Right = &TreeNode{Val: 4}
root.Right.Left = &TreeNode{Val: 0}
root.Right.Right = &TreeNode{Val: 8}
// Nodes for which we want to find LCA
A := root.Left // Node with value 5
B := root.Right // Node with value 1
// Find LCA of A and B
lca := lowestCommonAncestor(root, A, B)
if lca != nil {
fmt.Println("Lowest Common Ancestor of", A.Val, "and", B.Val, "is", lca.Val)
} else {
fmt.Println("No common ancestor found for", A.Val, "and", B.Val)
}
}
在这个示例中,lowestCommonAncestor 函数使用递归的方式来查找节点 A 和 B 的 LCA。在 main 函数中,构造了一个二叉树,并找到了节点 5 和节点 1 的最低公共祖先。
这段代码输出的结果应该是:
$ Lowest Common Ancestor of 5 and 1 is 3
这表明节点 5 和节点 1 的最低公共祖先是节点 3。
在给定的解决方案中,时间复杂度是 O(n),其中 n 是二叉树中节点的数量。
lowestCommonAncestor 可能会访问每个节点一次。这是因为在最差情况下,需要遍历整棵树来查找给定的两个节点 p 和 q。因此,整体来说,通过递归遍历二叉树来寻找两个节点的最低公共祖先的时间复杂度是 O(n),这保证了算法在合理的时间范围内解决问题,适用于一般大小的二叉树。
声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)进行许可,使用时请注明出处。
Author: mengbin
blog: mengbin
Github: mengbin92
cnblogs: 恋水无意
腾讯云开发者社区:孟斯特