avatarjb stevenard

Summary

The web content provides a detailed solution and explanation for finding the deepest node in a binary tree using a recursive depth-first search (DFS) approach in Swift.

Abstract

The article titled "10. Deepest Node in a Binary Tree" addresses the algorithmic challenge of identifying the deepest node within a binary tree structure. It presents a Swift function named deepestNode that utilizes a recursive depth-first search strategy to traverse the tree and maintain the depth of each node. The solution involves initializing a variable to keep track of the deepest node found and its depth, recursively exploring the tree's left and right subtrees, and updating the deepest node information based on the recursive calls' results. The article also includes a complexity analysis, indicating that the algorithm operates with a linear time complexity of O(n), where n is the number of nodes, and a space complexity of O(h), with h being the tree's height due to the call stack. Additionally, the article provides a test case to validate the function's correctness and suggests a follow-up problem to find the Kth deepest node.

Opinions

  • The author emphasizes the importance of comparing the height of nodes to determine the deepest one.
  • The use of recursion and DFS is suggested as an efficient method to browse the nodes of a binary tree.
  • The article promotes the idea that maintaining a tuple of the deepest node and its depth simplifies the tracking of the deepest node throughout the recursion.
  • The inclusion of a test case demonstrates the author's commitment to verifying the solution's accuracy.
  • The follow-up problem proposed by the author encourages further exploration and understanding of tree traversal algorithms.
  • The complexity analysis provided offers insight into the performance implications of the algorithm, highlighting its efficiency in terms of time and space.

10. Deepest Node in a Binary Tree

Question: Find the Deepest Node in a Binary Tree.

For example, you should return 4.

  1
 / \
 2 3
/
4

Hints:

- You need to compare the node’s height,

- To browse the nodes, use a DFS.

Solution:

fileprivate func deepestNode(node: BinaryNode<Int>) -> (BinaryNode<Int>, Int) {
    // 1.
    var deepest: (BinaryNode<Int>, Int) = (node, 0)
    // 2.
    if let left = node.left {
        deepest = deepestNode(node: left)
    }
    if let right = node.right {
        // 3.
        let deepestRight = deepestNode(node: right)
        if deepestRight.1 > deepest.1 {
            deepest = deepestRight
        }
    }
    // 4.
    return (deepest.0, deepest.1 + 1)
}

Explanations:

1. Let’s define the deepest node from the current node.

var deepest: (BinaryNode<Int>, Int) = (node, 0)

2. If the current node has a left child, let’s define the deepest from the result of the recursive call with its left child.

if let left = node.left {
  deepest = deepestNode(node: left)
}

3. If the current node has a right child, let’s compare the current deepest with the result of the recursive call from the right child and keep the deepest.

let deepestRight = deepestNode(node: right)
if deepestRight.1 > deepest.1 {
  deepest = deepestRight
}

4. Return the deepest and increment the current height.

return (deepest.0, deepest.1 + 1)

Complexity:

- Time Complexity: As we will browse all nodes once, the time complexity is linear O(n) n number being the number of nodes,

- Space Complexity: As we recurse on the tree, the time complexity will be due to the call stack; the time complexity will be O(h), where h is the height of the tree.

Test-cases:

public func testDeepestNode() {
  let n1 = BinaryNode<Int>(value: 1)
  let n2 = BinaryNode<Int>(value: 2)
  let n3 = BinaryNode<Int>(value: 3)
  let n4 = BinaryNode<Int>(value: 4)
  n1.left = n2
  n1.right = n3
  n2.left = n4
  assert(deepestNode(node: n1).0.value == 4, "deepestNode 1 not equal")
}

Follow-up:

Find the Kth deepest node.

<< More Interview Questions | Data Structures and Algorythms >>

Data Structures
Algorithms
Coding
Programming
Interview Questions
Recommended from ReadMedium