Press "Enter" to skip to content

Posts tagged as “recursion”

花花酱 LeetCode 64. Minimum Path Sum

Problem:

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example 1:

Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

 

Idea:

DP

Solution 1:

C++ / Recursion with memoization

Time complexity: O(mn)

Space complexity: O(mn)

Solution 2:

C++ / DP

Time complexity: O(mn)

Space complexity: O(1)

Related Problems:

 

花花酱 LeetCode 113. Path Sum II

Problem:

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

return

Idea:
Recursion
Solution:
C++

Related Problems:

花花酱 LeetCode 543. Diameter of Binary Tree

Problem:

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

 

Idea:

Recursion

Solution1:

C++

Solution 2: passed 101/106 TLE

C++ / Floyd-Warshall

Solution 3:

Simulate recursion with a stack. We also need to track the return value of each node.

Python

 

C++

 

Related Problems:

花花酱 LeetCode 687. Longest Univalue Path

题目大意:给你一棵二叉树,返回一条最长的路径,要求路径上所有的节点值都相同。

https://leetcode.com/problems/longest-univalue-path/description/

Problem: 

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:

              5
             / \
            4   5
           / \   \
          1   1   5

Output:

2

Example 2:

Input:

              1
             / \
            4   5
           / \   \
          4   4   5

Output:

2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.



Idea:

DFS

Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

Java

Python3

Related Problems

花花酱 LeetCode 669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

Example 2:

This problem can be solved with recursion

There 3 cases in total depends on the root value and L, R

Time complexity: O(n)

Space complexity: O(1)

Solution:

The previous solution has potential memory leak for languages without garbage collection.

Here’s the full program to delete trimmed nodes.

Example output