[LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4
Example 1:
5
1
3.
Example 2:
5
4
5
Note:
- All of the nodes' values will be unique.
- p and q are different and both values will exist in the binary tree.
与 235. Lowest Common Ancestor of a Binary Search Tree 类似,这道题是普通二叉树,不是二叉搜索树,所以不能利用二叉搜索树的性质,所以只能在二叉树中来搜索p和q,然后从路径中找到最近公共祖先。
解法:迭代
Java:
1 2 3 4 5 6 7 8 9 10 | public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null ) return null ; if (root == p || root == q) 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 ) ? left : right; } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution: # @param {TreeNode} root # @param {TreeNode} p # @param {TreeNode} q # @return {TreeNode} def lowestCommonAncestor( self , root, p, q): if root in ( None , p, q): return root left, right = [ self .lowestCommonAncestor(child, p, q) \ for child in (root.left, root.right)] # 1. If the current subtree contains both p and q, # return their LCA. # 2. If only one of them is in that subtree, # return that one of them. # 3. If neither of them is in that subtree, # return the node of that subtree. return root if left and right else left or right |
C++:Recursion
1 2 3 4 5 6 7 8 9 10 | class Solution { public : TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || p == root || q == root) return root; TreeNode *left = lowestCommonAncestor(root->left, p, q); TreeNode *right = lowestCommonAncestor(root->right, p , q); if (left && right) return root; return left ? left : right; } }; |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Solution { public : TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (!root || root == p || root == q) { return root; } TreeNode *left = lowestCommonAncestor(root->left, p, q); TreeNode *right = lowestCommonAncestor(root->right, p, q); // 1. If the current subtree contains both p and q, // return their LCA. // 2. If only one of them is in that subtree, // return that one of them. // 3. If neither of them is in that subtree, // return the node of that subtree. return left ? (right ? root : left) : right; } }; |
类似题目:
[LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步