236. Lowest Common Ancestor of a Binary Tree | [Super Important] | LCA of Binary Tree

Поделиться
HTML-код
  • Опубликовано: 27 авг 2024

Комментарии • 5

  • @Mounika-rz2ou
    @Mounika-rz2ou Месяц назад +4

    give him a crown🌟

  • @floatingpoint7629
    @floatingpoint7629 Месяц назад +1

    please solve this one: Selling Pieces of Wood

  • @TON-108
    @TON-108 Месяц назад

    Thank You 😊

  • @himanshigarg1431
    @himanshigarg1431 Месяц назад

    Thank you bhayia

  • @mystatuses1570
    @mystatuses1570 Месяц назад +2

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    if(root == null || root == p || root == q) return root;
    TreeNode leftLca = lowestCommonAncestor(root.left, p, q);
    TreeNode rightLca = lowestCommonAncestor(root.right, p, q);
    if(leftLca == null) return rightLca;
    if(rightLca == null) return leftLca;
    return root;
    }