LeetCode 1644. Lowest Common Ancestor of a Binary Tree II
Posted onInLeetcode
,
Tree Symbols count in article: 1.3kReading time ≈1 mins.
Question
Given the root of a binary tree, return the lowest common ancestor (LCA) of two given nodes,pandq. If either node p or qdoes not exist in the tree, return null. All values of the nodes in the tree are unique.
According to the definition of LCA on Wikipedia: βThe lowest common ancestor of two nodes p and q in a binary tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)β. A descendant of a node x is a node y that is on the path from node x to some leaf node.