- 浏览: 183699 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _ 2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
给定一个二叉树,找到他们的最近公共祖先,之前我们做过一道题目是从一个二叉搜索树中找两个节点的最近公共祖先,我们可以通过值的比较来找出。对于这道题目,我们设定两个辅助节点left和right,分别从根节点的左子树和右子树中寻找,找到了就返回相应的节点。最后判断left和right两个节点,如果都不为空,则LCA为root, 否则为left == null ? right : left。代码如下:
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _ 2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
给定一个二叉树,找到他们的最近公共祖先,之前我们做过一道题目是从一个二叉搜索树中找两个节点的最近公共祖先,我们可以通过值的比较来找出。对于这道题目,我们设定两个辅助节点left和right,分别从根节点的左子树和右子树中寻找,找到了就返回相应的节点。最后判断left和right两个节点,如果都不为空,则LCA为root, 否则为left == null ? right : left。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || p == root || q == root) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if(left != null && right != null) return root; else return left == null ? right : left; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 707For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Lowest Common Ancestor of a Binary Tree.java
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia:...
3. **二叉树的最近公共祖先 (Lowest Common Ancestor of a Binary Tree)**:这个是在非二叉搜索树的情况下的类似问题,通常需要额外的策略来处理。 为了有效地解决这些问题,你需要熟悉二叉搜索树的遍历方法,包括...
c c语言_leetcode题解之0236_lowest_common_ancestor_of_a_binary_tree
比如"二叉树的最近公共祖先"(Lowest Common Ancestor of a Binary Tree)需要掌握树的遍历策略,而"有效的井字游戏"(Valid Tic-Tac-Toe)则需要理解博弈论的Nim游戏规则。 每道题的Markdown文件通常包含以下几个...
5. **二叉树与图**:二叉树题目如"Binary Tree Inorder Traversal"(二叉树的中序遍历)和"Lowest Common Ancestor of a Binary Tree"(二叉树的最近公共祖先),图题目如"Shortest Path in Bidirectional Graph"...
2. 树形结构:树相关的题目如“Binary Tree Inorder Traversal”(二叉树的中序遍历)和“Lowest Common Ancestor of a Binary Tree”(二叉树的最近公共祖先)等,要求对二叉树的遍历和搜索有深入理解。 3. 排序与...
例如,"二叉树的最近公共祖先"(Lowest Common Ancestor of a Binary Tree)题目,需要理解深度优先搜索或层次遍历的方法。 高级算法: 高级算法通常涉及到更深层次的思考和优化,如位运算、贪心策略、分治法等。...
2. 二叉树:例如“Lowest Common Ancestor of a Binary Tree”涉及到二叉树的遍历和节点查找。 3. 图论:如“Course Schedule II”可能涉及拓扑排序和深度优先搜索。 4. 字符串处理:如“Valid Palindrome II”要求...
4. **树结构**:如“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree),需要找到给定节点的最近公共祖先。 5. **动态规划**:如“最长连续序列”(Longest Consecutive Sequence),要求找到一个...
"二叉树的最近公共祖先"(Lowest Common Ancestor of a Binary Tree)可以用递归或迭代的方法解决。 5. **图**:图问题可能涉及到深度优先搜索(DFS)、广度优先搜索(BFS)等。例如,"最短路径"(Shortest Path in...
如“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree)和“拓扑排序”(Topological Sort)。 8. **动态规划**:动态规划用于解决具有重叠子问题和最优子结构的问题,如“背包问题”(Knapsack ...
例如,“二叉树的最近公共祖先”(Lowest Common Ancestor of a Binary Tree)问题,要求找到二叉树中两个节点的最近公共祖先。 8. **动态规划**:这是一种优化问题解决的策略,常用于解决背包问题、最长公共子序列...
Lowest Common Ancestor of a Binary Search Tree等。通过解决这些题目,你可以加深对二叉树的理解,并提升解决问题的能力。 总结,Java中的二叉树题目不仅要求对数据结构有深入理解,还需要灵活运用算法。不断...
12. **Lowest Common Ancestor of a Binary Search Tree**:二叉搜索树的最近公共祖先。利用二叉搜索树的性质,可以有效地向上遍历找到最近公共祖先。 13. **Product of Array Except Self**:不包含自身的数组乘积...
**RMQ(Range Minimum Query)与LCA(Lowest Common Ancestor)问题**是图论与数据结构领域中的两个重要概念,广泛应用于算法设计和优化。这篇由郭华阳所著的国家队论文深入探讨了这两个问题及其解决方案。 **RMQ...
要在控制台中运行特定问题,请转至文件test,将调用添加到测试函数( test() ),然后在控制台node <problem> (例如, node LeetcodeProblems/Lowest_Common_Ancestor_of_a_Binary_Tree.js )。 Leetcode问题 名称...
问题235:“最近的公共祖先(Lowest Common Ancestor of a Binary Search Tree)” 这是一个关于二叉搜索树的问题。在二叉搜索树中,每个节点的值都大于其左子树中的所有节点值,小于其右子树中的所有节点值。这个问题...
最近公共祖先(Lowest Common Ancestor,简称LCA)问题在图论和算法中具有重要的地位,特别是在处理树形结构的数据时。LCA问题指的是在一个树形结构中,查找两个指定节点的最近公共祖先,即离这两个节点最近的共同父...
2. **LCA (Lowest Common Ancestor)**:在树结构中,LCA算法用于寻找两个节点的最近公共祖先。递归和非递归版本都存在,递归版本通过自底向上的方式计算,非递归版本通常使用预处理和Mo's Algorithm来优化。 3. **...