求一棵树中两个节点的最近祖先(Java代码)

假设给了o1和o2两个节点(节点属于同一棵树中),求两个节点的最近公共祖先。存在两种情况:1)、o1是o2的祖先或者o2是o1的祖先2)、两者都不是对方的祖先。方法一:用HashMap和HashSet:先将o1的祖先串联起来,然后在遍历o2 的祖先,找到第一个与o1的祖先相同的节点就两者共同的最近祖先。public static Node lowestAncestor(Node head, Node o1, Node o2){ HashMap<Node, Node> fathe
摘要由CSDN通过智能技术生成

假设给了o1和o2两个节点(节点属于同一棵树中),求两个节点的最近公共祖先。
存在两种情况:
1)、o1是o2的祖先或者o2是o1的祖先
2)、两者都不是对方的祖先。

方法一:用HashMap和HashSet:先将o1的祖先串联起来,然后在遍历o2 的祖先,找到第一个与o1的祖先相同的节点就两者共同的最近祖先。

public static Node lowestAncestor(Node head, Node o1, Node o2){
   
    HashMap<Node, Node> fatherMap = new HashMap<>();
    //特殊处理,头节点的父节点是自己
    fatherMap.put(head, head);
    process(head, fatherMap);
    HashSet<N
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是利用栈实现任意两个节点最近公共祖先的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 定义栈结构体 typedef struct Stack { TreeNode *data[MAX_SIZE]; int top; } Stack; // 初始化栈 void initStack(Stack *s) { s->top = -1; } // 判断栈是否为空 int isEmpty(Stack *s) { return s->top == -1; } // 判断栈是否已满 int isFull(Stack *s) { return s->top == MAX_SIZE - 1; } // 入栈 void push(Stack *s, TreeNode *node) { if (isFull(s)) { printf("Stack is full!\n"); return; } s->data[++s->top] = node; } // 出栈 TreeNode *pop(Stack *s) { if (isEmpty(s)) { printf("Stack is empty!\n"); return NULL; } return s->data[s->top--]; } // 获取栈顶元素 TreeNode *getTop(Stack *s) { if (isEmpty(s)) { printf("Stack is empty!\n"); return NULL; } return s->data[s->top]; } // 判断节点是否在二叉树 int isNodeInTree(TreeNode *root, TreeNode *node) { if (root == NULL) { return 0; } if (root == node) { return 1; } return isNodeInTree(root->left, node) || isNodeInTree(root->right, node); } // 查找任意两个节点最近公共祖先 TreeNode *lowestCommonAncestor(TreeNode *root1, TreeNode *root2, TreeNode *p, TreeNode *q) { // 定义两个栈,用于存储从根节点节点p和节点q的路径 Stack stack1, stack2; initStack(&stack1); initStack(&stack2); // 从根节点开始遍历两棵树,分别找到节点p和节点q,并将路径存储到对应的栈 TreeNode *cur1 = root1; while (cur1 || !isEmpty(&stack1)) { while (cur1) { push(&stack1, cur1); if (cur1 == p || cur1 == q) { break; } cur1 = cur1->left; } cur1 = getTop(&stack1); if (cur1->right && cur1->right != pop(&stack1)) { cur1 = cur1->right; } else { if (cur1 == p || cur1 == q) { break; } cur1 = NULL; } } TreeNode *cur2 = root2; while (cur2 || !isEmpty(&stack2)) { while (cur2) { push(&stack2, cur2); if (cur2 == p || cur2 == q) { break; } cur2 = cur2->left; } cur2 = getTop(&stack2); if (cur2->right && cur2->right != pop(&stack2)) { cur2 = cur2->right; } else { if (cur2 == p || cur2 == q) { break; } cur2 = NULL; } } // 如果节点p或节点q不在任何一棵树,则它们没有公共祖先 if (!isNodeInTree(root1, p) || !isNodeInTree(root1, q) || !isNodeInTree(root2, p) || !isNodeInTree(root2, q)) { return NULL; } // 从栈顶开始比较两个的元素,找到最后一个相同的节点即为最近公共祖先 TreeNode *ancestor = NULL; while (!isEmpty(&stack1) && !isEmpty(&stack2)) { TreeNode *node1 = pop(&stack1); TreeNode *node2 = pop(&stack2); if (node1 == node2) { ancestor = node1; } else { break; } } return ancestor; } // 创建二叉树 TreeNode *createBinaryTree(int arr[], int index, int n) { if (index >= n || arr[index] == -1) { return NULL; } TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); root->val = arr[index]; root->left = createBinaryTree(arr, 2 * index + 1, n); root->right = createBinaryTree(arr, 2 * index + 2, n); return root; } int main() { // 测试数据 int arr1[] = {3, 5, 1, 6, 2, 0, 8, -1, -1, 7, 4}; int arr2[] = {5, 6, 2, -1, -1, 7, 4}; TreeNode *root1 = createBinaryTree(arr1, 0, sizeof(arr1) / sizeof(arr1[0])); TreeNode *root2 = createBinaryTree(arr2, 0, sizeof(arr2) / sizeof(arr2[0])); TreeNode *p = root1->left; TreeNode *q = root2->left->right; // 查找任意两个节点最近公共祖先 TreeNode *ancestor = lowestCommonAncestor(root1, root2, p, q); if (ancestor != NULL) { printf("The lowest common ancestor of node %d and node %d is %d.\n", p->val, q->val, ancestor->val); } else { printf("Node %d and node %d have no common ancestor.\n", p->val, q->val); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值