Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does Java determine whether an array is a post-order traversal result?

Shulou Source: shulou.com Published: 2022-05-31 17:55:51 09月30日 Update

This article mainly explains "Java how to judge whether an array is a post-order traversal result". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "Java how to determine whether an array is a post-order traversal result"!

Enter an array of integers to determine whether the array is the result of a post-traversal of a binary search tree. If true is returned, otherwise false is returned.

Idea 1:

The middle order is traversed as a growth array to determine whether it is contradictory or not.

Idea 2:

Such as 5, 7, 6, 9, 11, 10, 8

Specific ideas for writing code:

1. Find the first number greater than the root node, that is, 9, so the one after 9 is the right subtree.

two。 If the value of the right subtree is greater than the root node 8, the

3. The recursive method is used to determine whether the left subtree and the right subtree are in line with this characteristic.

Package com.lifeibigdata.algorithms.blog;import java.util.Arrays;/** 5, 7, 6, 9, 11, 10, 8 * 8 /\ 6 10 /\ 5 7 9 11 * / public class SearchTree {public static void main (String [] args) {/ / int [] a = {5, 7, 6, 9, 10}; / / true int a [] = {7,4,6,5} / / false System.out.println (searchTree (amema.length));} static boolean searchTree (int [] apenint length) {if (a = = null | | length 0) {searchTree (apeny I);} if (I < length-1) {searchTree (Arrays.copyOfRange (adint length-1), length-I-1);} return flag }} at this point, I believe you have a deeper understanding of "how Java determines whether an array is a post-order traversal result". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Subtrees arrays results ideas content nodes learning practicality deeper contradictions code interests boundaries practicality practicality simple operation integers methods more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology Redmi Linux OPPO Reno