Calculate the structure of a binary tree based on the two traversal methods of the binary tree.

Source: Internet
Author: User


The pre-order traversal order of the binary tree is: root node, left Tree, right tree

The central traversal order is: left Tree, root node, right tree

The descending order of traversal is: left Tree, right tree, and root node.



650) This. width = 650; "width =" 621 "Height =" 296 "style =" width: 482px; Height: 274px; "src =" http://img.blog.csdn.net/20141022130455285? Watermark/2/text/plain =/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma =/dissolve/70/gravity/center "/>


In the preceding tree, the forward traversal is: abfcjm; the forward traversal is: fbcamj; the backward traversal is: fcbmja.


The unique structure of the tree cannot be obtained based on the pre-order traversal and post-order traversal,

The two known traversal orders must include the central traversal, because the central traversal can recursively launch the left and right trees of the root node.

For the above question, we know that the forward traversal is: the forward traversal in abfcjm is: fbcamj, then we can know that the root of the tree is a according to the forward traversal,

Then, traverse through the middle order to know that the left Tree node with "A" as the root node includes FBC right Tree nodes including MJ,

Then, we can see that B is the left subnode of a according to the second node traversed in the previous order, and that the left Tree of B is F and the right tree is C according to the middle order traversal,

Repeat the preceding steps for Recursive solution to release the entire tree structure,

Finally, the order of convenience can be introduced based on the structure of the entire tree.



This article is from the "Developer" blog, please be sure to keep this source http://tang1513.blog.51cto.com/7678175/1566669

Calculate the structure of a binary tree based on the two traversal methods of the binary tree.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.