The chicken dish brush Leetcode 236. Lowest Common Ancestor of a Binary Tree Medium

236. Lowest Common Ancestor of a Binary Tree  Medium

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 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]

Example 1:

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

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 5 and 4 is 5, 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 binary tree.

analysis:

First of all, the problem tree, often involving recursion, the reason is because each involves a recursive tree is composed of a plurality of sub-tree of the same structure. This problem can be used recursively, starting from the leaf nodes corresponding to recursively returns the following information: the current tree (or subtree) contains both p and q, the function returns the result that their lowest common ancestor. If one of them among the subtree, the result is one of them. If they are not in the sub-tree, the result returns NULL.

class Solution {
 public : 
    the TreeNode * lowestCommonAncestor (the TreeNode * the root, the TreeNode * P, the TreeNode * Q) {
         // Although if the following two can be written together, but the meaning of the expression is not the same 
        if (the root == nullptr a) {
             return nullptr a; 
        } 
        IF (the root || P == Q == the root) {
             return the root; 
        } 
        the TreeNode * = lowestCommonAncestor left (directory root-> left, P, Q); 
        the TreeNode * = right lowestCommonAncestor (directory root-> right, P, Q);
         IF (left == nullptr a) {   //If a branch is empty, the other is not empty, then the node does not return empty 
            return right; 
        } 
        the else  IF (right == nullptr a) {
             return left; 
        } 
        the else   // If the two are not empty, return both the current lowest ancestor node 
            return root; 
    } 
};

to sum up:

This question is the book of the practice of trouble. First, the book is not a binary tree, but an ordinary tree, the next there is a vector in.
Second, the approach of the book is to find p and q are two paths, starting from the root node to p and q, respectively, constitute two lists;
and finally, the first place to look for a bifurcation of these two list.

Note:

If the tree is the theme of this binary search tree or pointing the parent node pointer, then the idea can be more simple.

Guess you like

Origin www.cnblogs.com/Flash-ylf/p/11494780.html