Skip to content

236.二叉树的最近公共祖先

题解

js
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
  if (!root || p === root || q === root) return root;
  const leftNode = lowestCommonAncestor(root.left, p, q);
  const rightNode = lowestCommonAncestor(root.right, p, q);
  // 如果左子树找到了节点,右子树也找到了节点,当前节点就是祖先节点
  if (leftNode && rightNode) return root;
  return leftNode || rightNode;
};
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
var lowestCommonAncestor = function (root, p, q) {
  if (!root || p === root || q === root) return root;
  const leftNode = lowestCommonAncestor(root.left, p, q);
  const rightNode = lowestCommonAncestor(root.right, p, q);
  // 如果左子树找到了节点,右子树也找到了节点,当前节点就是祖先节点
  if (leftNode && rightNode) return root;
  return leftNode || rightNode;
};