LeetCode 235. Lowest Common Ancestor of a Binary Search Tree

原题链接在这里:https://leetcode.com/problems/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: “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 binary search tree:  root = [6,2,8,0,4,7,9,null,null,3,5]

Example 1:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.

Example 2:

Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the BST.

题解:

Top down DFS. 

SFS state needs current node, target p and q.

When current node val < p.val and current node val < q.val, then LCA must be in the right branch, continue on right branch.

If current node val > p.val and current node > q.val, then LCA must be in the left branch, continue on the left branch.

Else, current node is between p.val and q.val, and current node is LCA.

只有当root的val在p, q的val中间包括p, q的val时,返回root.

Time Complexity: O(h), h is height of tree.

Space: O(h), 用了O(h)层stack.

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.val < p.val && root.val < q.val){
16             return lowestCommonAncestor(root.right,p,q);
17         }else if(root.val > p.val && root.val > q.val){
18             return lowestCommonAncestor(root.left,p,q);
19         }else{
20             return root;
21         }
22     }
23 }

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 
11 class Solution {
12 public:
13     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
14         if(!root){
15             return root;
16         }
17 
18         if(p->val < root->val && q->val < root->val){
19             return lowestCommonAncestor(root->left, p, q);
20         }else if(p->val > root->val && q->val > root->val){
21             return lowestCommonAncestor(root->right, p ,q);
22         }else{
23             return root;
24         }
25     }
26 };

跟上Lowest Common Ancestor of a Binary Tree.

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