🇺🇿O'zbekchada pastda⬇️
🇬🇧
Today I would like to briefly discuss Depth First Search (DFS). Usually, I use this recursive approach to solve problems with trees. You should consider the repetitive case/action which you apply on each iteration/node. Also, don't forget about the base case, when you should stop your recursive calls.
Basically, DFS recursive method I applied to trees 🎄 usually looks like this:
And my solution (0️⃣ ms, 1️⃣0️⃣0️⃣% faster in Java ☕️): solution link
As you can see this algorithm is pretty useful for traversing trees. Don't run into StackOverFlow 😁
If you find this useful, then subscribe for more
Tags:
#binarytree binary tree
🇬🇧
Today I would like to briefly discuss Depth First Search (DFS). Usually, I use this recursive approach to solve problems with trees. You should consider the repetitive case/action which you apply on each iteration/node. Also, don't forget about the base case, when you should stop your recursive calls.
Basically, DFS recursive method I applied to trees 🎄 usually looks like this:
private void traverseBT(TreeNode node) {
//1. Base case or case when you should stop recursive calls
if (node == nulll) {
return;
}
//2. Some action with node or its value
//3. Apply the same action to the left child
traverseBT(node.left);
//and to the right child
traverseBT(node.right);
}
For example, a problem: Problem 100And my solution (0️⃣ ms, 1️⃣0️⃣0️⃣% faster in Java ☕️): solution link
As you can see this algorithm is pretty useful for traversing trees. Don't run into StackOverFlow 😁
If you find this useful, then subscribe for more
Tags:
#binarytree binary tree
LeetCode
Same Tree - LeetCode
Can you solve this real interview question? Same Tree - Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the…
Two binary trees are considered the same if they are structurally identical, and the nodes have the…
🔥5👍1
🇺🇿O'zimizchasi
Hammaga salom.
Bugun Depth First Search (DFS) haqida qisqacha yozmoqchiman. Meningcha, bu LeetCode da juda ham ko'p ishlatiladigan yondashuv. Odatda bu rekursiv yondashuvni men tree (daraxt) lar bilan bog'liq misollarni yechish uchun ishlataman. Bu uslub ishlatganda har bir iteratsiya/tugunda qo'llanadigan takrorlanuvchi holatni/harakatni ko'rib chiqishingiz kerak. Bundan tashqari, rekursiv chaqirishlarni to'xtatish kerak bo'lgan asosiy holat haqida unutmang.
Asosan, men daraxtlarga 🎄 (tree) qo'llaydigan DFS rekursiv usuli odatda shunday ko'rinishga ega:
Va mening yechimim (Javada 0️⃣ ms, 1️⃣0️⃣0️⃣% tezroq ☕️): yechim havolasi
Ko'rib turganingizdek, bu algoritm daraxtlarni kesib o'tish uchun juda foydali. StackOverFlow haqida unutmang 😁
Agar siz buni foydali deb topsangiz, obuna bo'ling
Tags:
#binarytree binary tree
Hammaga salom.
Bugun Depth First Search (DFS) haqida qisqacha yozmoqchiman. Meningcha, bu LeetCode da juda ham ko'p ishlatiladigan yondashuv. Odatda bu rekursiv yondashuvni men tree (daraxt) lar bilan bog'liq misollarni yechish uchun ishlataman. Bu uslub ishlatganda har bir iteratsiya/tugunda qo'llanadigan takrorlanuvchi holatni/harakatni ko'rib chiqishingiz kerak. Bundan tashqari, rekursiv chaqirishlarni to'xtatish kerak bo'lgan asosiy holat haqida unutmang.
Asosan, men daraxtlarga 🎄 (tree) qo'llaydigan DFS rekursiv usuli odatda shunday ko'rinishga ega:
private void traverseBT(TreeNode node) {
//1. Asosiy holat
if (node == nulll) {
return;
}
//2. Daraxt tuguni (tree node) yoki uning qiymati bilan ba'zi harakatlar
//3. Xuddi shu harakatni chap bolasiga rekursiv chaqirish orqali qo'llang
traverseBT(node.left);
//va xuddi shu harakatni o'ng bolasiga rekursiv chaqirish orqali qo'llang
traverseBT(node.right);
}
Shu algoritm uchun misol: Problem 100Va mening yechimim (Javada 0️⃣ ms, 1️⃣0️⃣0️⃣% tezroq ☕️): yechim havolasi
Ko'rib turganingizdek, bu algoritm daraxtlarni kesib o'tish uchun juda foydali. StackOverFlow haqida unutmang 😁
Agar siz buni foydali deb topsangiz, obuna bo'ling
Tags:
#binarytree binary tree
LeetCode
Same Tree - LeetCode
Can you solve this real interview question? Same Tree - Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the…
Two binary trees are considered the same if they are structurally identical, and the nodes have the…
🔥13👍2