Tag Archives: binary tree

Leetcode Çözümleri | Binary Tree Inorder Traversal

Leetcode içerisinde bulunan “Binary Tree Inorder Traversal” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘nin node’larını inorder traversal, yani öncelikle rekürsif olarak sol tarafı, sonra head nodu, sonra da rekürsif olarak sağ tarafı geri döndürmeniz isteniyor.

➡️ Tree traversal: https://en.wikipedia.org/wiki/Tree_traversal

➡️ Leetcode 94. Binary Tree Inorder Traversal: https://leetcode.com/problems/binary-tree-inorder-traversal/

➡️ Problem açıklaması:

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1:

Input: root = [1,null,2,3]

Output: [1,3,2]

Example 2:

Input: root = []

Output: []

Example 3:

Input: root = [1]

Output: [1]

Constraints:

The number of nodes in the tree is in the range [0, 100].

-100 <= Node.val <= 100

Follow up: Recursive solution is trivial, could you do it iteratively?

Linkedin Mülakat Sorusu – Maximum Depth of Binary Tree

LeetCode içerisinde bulunan “Maximum Depth of Binary Tree” sorusunun açıklaması ve çözümü. Bu soruda sizi verilen bir binary tree içerisinde, root node‘undan en uzaktaki leaf node‘una olan uzaklığı bulmanız isteniyor.

► LeetCode 104. Maximum Depth of Binary Tree: https://leetcode.com/problems/maximum-depth-of-binary-tree/

► Problem açıklaması:

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

Input: root = [3,9,20,null,null,15,7]

Output: 3

Example 2:

Input: root = [1,null,2]

Output: 2

Example 3:

Input: root = []

Output: 0

Example 4:

Input: root = [0]

Output: 1

Constraints:

The number of nodes in the tree is in the range [0, 10^4].

-100 <= Node.val <= 100

LeetCode Çözümleri – 101. Symmetric Tree

LeetCode içerisinde bulunan “Symmetric Tree” sorusunun açıklaması ve çözümü. Size root’u verilen bir binary tree‘nin merkezine göre simetrik olup olmadığını bulmanız isteniyor.

► LeetCode 101. Symmetric Tree: https://leetcode.com/problems/symmetric-tree/

► Problem açıklaması:

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Input: root = [1,2,2,3,4,4,3]

Output: true

Input: root = [1,2,2,null,3,null,3]

Output: false

Constraints:

The number of nodes in the tree is in the range [1, 1000].

-100 <= Node.val <= 100

Follow up: Could you solve it both recursively and iteratively?

LeetCode Çözümleri – 226. Invert Binary Tree

LeetCode içerisinde bulunan “Invert Binary Tree” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir binary tree‘i tersine çevirmeniz isteniyor. Binary tree yapısı ile en sık karşılaşabileceğiniz soru tipi. Rekürsif yapı ile mantığını oturtursanız çok kolay anlaşılabiliyor.

✨ Binary tree nedir?

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

➡️ Binary Tree Data Structure: https://www.geeksforgeeks.org/binary-tree-data-structure/

➡️ Binary Trees: https://www.cmpe.boun.edu.tr/~akin/cmpe223/chap4.htm

➡️ Binary Trees by Nick Parlante: http://cslibrary.stanford.edu/110/BinaryTrees.html

✨ LeetCode 226. Invert Binary Tree: https://leetcode.com/problems/invert-binary-tree/

➡️ Problem açıklaması:

Invert a binary tree.

Example:

Input:

4

/ \

2 7

/ \ / \

1 3 6 9

Output:

4

/ \

7 2

/ \ / \

9 6 3 1

Trivia:

This problem was inspired by this original tweet (https://twitter.com/mxcl/status/608682016205344768) by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.