★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-txyjzppd-mc.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.node
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).」ios
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]git
Example 1:github
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes and is 513.
Example 2:微信
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes and is , since a node can be a descendant of itself according to the LCA definition. 545
Note:spa
給定一個二叉樹, 找到該樹中兩個指定節點的最近公共祖先。code
百度百科中最近公共祖先的定義爲:「對於有根樹 T 的兩個結點 p、q,最近公共祖先表示爲一個結點 x,知足 x 是 p、q 的祖先且 x 的深度儘量大(一個節點也能夠是它本身的祖先)。」htm
例如,給定以下二叉樹: root = [3,5,1,6,2,0,8,null,null,7,4]blog
示例 1:
輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 輸出: 3 解釋: 節點 和節點 的最近公共祖先是節點 513。
示例 2:
輸入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 輸出: 5 解釋: 節點 和節點 的最近公共祖先是節點 由於根據定義最近公共祖先節點能夠爲節點自己。 545。
說明:
遞歸
1 class Solution { 2 func lowestCommonAncestor(_ root: TreeNode?, _ p: TreeNode?,_ q: TreeNode?) -> TreeNode? { 3 if root!.val == nil || root!.equal(p) || root!.equal(q) 4 { 5 return root 6 } 7 8 var left:TreeNode? = lowestCommonAncestor(root!.left, p, q) 9 if left!.val != nil && left!.equal(p) && left!.equal(q) 10 { 11 return left 12 } 13 14 var right:TreeNode? = lowestCommonAncestor(root!.right, p , q) 15 if left!.val != nil && right!.val != nil 16 { 17 return root 18 } 19 return left!.val != nil ? left : right 20 } 21 } 22 public class TreeNode { 23 public var val: Int 24 public var left: TreeNode? 25 public var right: TreeNode? 26 public init(_ val: Int) { 27 self.val = val 28 self.left = nil 29 self.right = nil 30 } 31 32 func equal(_ root: TreeNode?)-> Bool 33 { 34 return (self.val == root!.val) && (self.left!.val == root!.left!.val) && (self.right!.val == root!.right!.val) 35 } 36 }
C++:4ms
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 static const auto _____ = []() 11 { 12 ios::sync_with_stdio(false); 13 cin.tie(nullptr); 14 return nullptr; 15 }(); 16 class Solution { 17 public: 18 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 19 if (root == NULL || root == p || root == q) return root; 20 TreeNode* ptr1 = lowestCommonAncestor(root->left, p, q); 21 TreeNode* ptr2 = lowestCommonAncestor(root->right, p, q); 22 if (ptr1 && ptr2) return root; 23 return ptr1 ? ptr1 : ptr2; 24 } 25 };
C++:12ms
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 13 if(!root||p==root||q==root) return root; 14 TreeNode *left = lowestCommonAncestor(root->left, p, q); 15 TreeNode *right = lowestCommonAncestor(root->right, p, q); 16 if (left && right) return root; 17 return left ? left : right; 18 19 } 20 };