1650. (LCA) Lowest Common Ancestor of a Binary Tree III
้้ก่ทๅๅ ฉ้กไธๅ่ๆฏ.., node ๆฌ่บซๆ parent info,
่ไธๆฒๆ root input, ๆไปฅ่ฆๅฉ็จ parent ไพๆพ
has parent info, use hashset to stroe parent info
time: O(h), worst case is from leaf to root, is tree's height
space: O(h), same
ๅ ็บๆ parent, ๆไปฅ่ทไนๅ็ๆฆๅฟต้ทๅ, ๅฆๆ็จๆดๅๆณ็ๆฆๅฟตๅปๆณ(ไนๅๆๆๅฐ,
LCA ๆๅบ็พๅพ p ๅพไธ, q ๅพไธ, ไบคๆไน่
ๆไปฅๅฏไปฅๅ ๆ p ๅพไธ็ ่ทฏๅพ๏ผparent ่ทฏๅพ)่จ่ตทไพ, q ๅพไธ็ ่ทฏๅพ, ็ดๆฅๅปๆฏๅฐ, ้ฃๆพๅฐๅฐฑๆฏ LCA
Last updated