LeetCode 236. Lowest Common Ancestor of a Binary Tree

原题链接在这里:https://leetcode.com/problems/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 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.

题解:

Lowest Common Ancestor of a Binary Search Tree进阶题目。

无法比较大小,但是可以看p,q是不是在root的两边,若在两边,left 和 right 同时不失null, 则返回root.

若都在一边,比如left, 就在left边继续。

Note:1. 若有root == p || root == q时,需比较原来的点而不单单是val, 这里可以有重复的值,在能比较整体点时就不比较val.

2. 递归终止条件这里有两个,一个是root == null, 一个是root等于p或者q, 这两个终止条件缺一不可。

Time Complexity: O(n), 每个点没有traverse超过两遍. Space: O(logn), 是树的高度。

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12         if(root == null){
13             return root;
14         }
15         if(root == p || root == q){
16             return root;
17         }
18         
19         TreeNode left = lowestCommonAncestor(root.left, p, q);
20         TreeNode right = lowestCommonAncestor(root.right, p, q);
21         
22         if(left != null && right != null){
23             return root;
24         }else if(left != null){
25             return left;
26         }else{
27             return right;
28         }
29     }
30 }

AC C++:

 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){
14             return root;
15         }
16 
17         if(root == p || root == q){
18             return root;
19         }
20 
21         TreeNode* l = lowestCommonAncestor(root->left, p, q);
22         TreeNode* r = lowestCommonAncestor(root->right, p, q);
23         if(l && r){
24             return root;
25         }else if(l){
26             return l;
27         }else{
28             return r;
29         }
30     }
31 };

类似Lowest Common Ancestor of a Binary Search TreeSmallest Common Region.

跟上Smallest Subtree with all the Deepest NodesLowest Common Ancestor of Deepest LeavesLowest Common Ancestor of a Binary Tree IILowest Common Ancestor of a Binary Tree IIILowest Common Ancestor of a Binary Tree IVFind Distance in a Binary Tree.

posted @ 2015-09-05 05:11  Dylan_Java_NYC  阅读(405)  评论(0编辑  收藏  举报