With the development of cloud technology, hierarchical and distributed cross-cloud architecture is gradually replacing traditional centralized architecture, for example, used in edge (or fog) computing. Due to the fluctuation of resource requirements, if a node does not have sufficient resources to process requests, the same or higher-level nodes can share their resources by offloading or redirecting requests to themselves, at the possible cost of reduced service quality. However, it is difficult to effectively optimize the sharing effect based on mean requirements. We formulate the multilevel problem with horizontal and vertical resource sharing using stochastic models, identify the optimal structures with embedded subproblems, and obtain the approximation solution in an efficient dynamic programming manner. In the problem setting with a wide range of different parameters, the proposed algorithm can outperform existing mean and heuristic algorithms in all scenarios to improve the total satisfied requirements by up to 26%, and can be hundreds of times faster than these heuristic algorithms.