一、 

1. Lowest Common Ancestor

 1 class Solution {
 2 public:
 3     TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
 4         if (root == NULL || root == A || root == B) {
 5             return root;
 6         }
 7         TreeNode* left = lowestCommonAncestor(root->left, A, B);
 8         TreeNode* right = lowestCommonAncestor(root->right, A, B);
 9         if (left != NULL && right != NULL) {
10             return root;
11         }
12         if (left != NULL) {
13             return left;
14         }
15         if (right != NULL) {
16             return right;
17         }
18         return NULL;
19     }
20 };
View Code

相关文章:

  • 2021-10-22
  • 2022-12-23
  • 2022-01-10
  • 2021-10-08
  • 2022-01-07
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2021-10-06
  • 2022-02-13
相关资源
相似解决方案