LeetCode-Binary Tree Postorder Traversal, binarysearchtree

Source: Internet
Author: User

LeetCode-Binary Tree Postorder Traversal, binarysearchtree

Title: https://oj.leetcode.com/problems/binary-tree-postorder-traversal/

Given a binary tree, return the postorder traversal of its nodes 'values.

For example:
Given binary tree{1,#,2,3},

   1    \     2    /   3

Return[3,2,1].

Note: Recursive solution is trivial, cocould you do it iteratively?

Source code: Java version
Algorithm Analysis: Double stack. Time complexity O (n), space complexity O (n)

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List<Integer> postorderTraversal(TreeNode root) {        List<Integer> result=new ArrayList<Integer>();          Stack<TreeNode> stack=new Stack<TreeNode>();         Stack<Integer> cntStack=new Stack<Integer>();        TreeNode node=root;          int label;        do {            while(node!=null) {                stack.push(node);                cntStack.push(0);                node=node.left;            }            if(!stack.isEmpty()) {                node=stack.pop();                label=cntStack.pop();                if(label==0) {                    cntStack.push(1);                    stack.push(node);                    node=node.right;                }else {                    result.add(node.val);                    node=null;  //import                }            }                    }while(!stack.isEmpty());                return result;    }}
Code explanation: the cntStack is used to indicate whether the vertex is first or second out of the stack. For the first time, the stack needs to be accessed by the node, and for the second time, the stack needs to be accessed directly.


Valentain tree Origin

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.

This is an example of one of her creations:
D
/\
/\
B E
/\\
/\\
A C G
/
/
F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree ). for the tree drawn abve the preorder traversal is dbacepidermal and the inorder traversal is ABCDEFG.

She thought that such a pair of strings wocould give enough information to reconstruct the tree later (but she never tried it ).

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.

However, doing the reconstruction by hand, soon turned out to be tedious.

So now she asks you to write a program that does the job for her!

Input

The input will contain in one or more test cases.

Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. both strings consist of unique capital letters. (Thus they are not longer than 26 characters .)

Input is terminated by end of fil... the remaining full text>
 
What's wrong with the WordPress 548-Tree?

The AC code. In the question, we will give you a binary tree's central and backward traversal.
The nodes here are in the range of 1 to 10000, with a maximum of 10000 nodes. However, the nodes here are not consecutive, not that there are N nodes that are numbered from 1 to N.
For example
1 7 8
1, 8, and 7.
Below is my AC code
# Include <cstdlib>
# Include <stdio. h>
# Include <string. h>
Const int MAX = 10005;
Int in [MAX];
Int post [MAX];
Int son [MAX] [2];
Char s [1000000];
Int p [MAX];
Int ind, dis [MAX];
Int deal (int x [], char s [])
{
Int n = 0, I;
For (I = 0; s [I]; I ++)
{
If (s [I]> = '0' & s [I] <= '9 ')
{
X [n] = 0;
While (s [I] & s [I]> = '0' & s [I] <= '9 ')
{
X [n] = x [n] * 10 + s [I]-'0 ';
I ++;
}
I --;
N ++;
}
}
Return n;
}
Int DFS (int low, int high)
{
If (low> = high) return-1;
If (ind =-1) return-1;
Int r = post [ind];
Int rp = p [r];
If (rp> = low & rp {
Ind --;

Son [r] [1] = DFS (rp + 1, high );
Son [r] [0] = DFS (low, rp );
Return r;
}
Else return-1;
}
Void getDis (int r)
{
Int t;
If (son [r] [0]! =-1)
{
T = son [r] [0];
Dis [t] = dis [r] + t;
GetDis (t );
}
If (son [r] [1]! =-1)
{
T = son [r] [1];
Dis [t] = dis [r] + t;
GetDis (t );
}
}
Void inorder (int r)
{
If (son [r] [0]! =-1) inorder (son [r] [0]);
Printf ("% d", r );
If (son [r] [1]! =-1) inorder (son [r] [1]);
}
Void postorder (int r)
{
If (son [r] [0]! =-1) postorder (son [r] [0]);

If (son [r] [1]! =-1) postorder (son [r] [1]);
Printf ("% d", r );
}
Int main ()
{
Int n, I, k;
While (gets (s ))
{
N = deal (in, s );
Gets (s );
N = deal (post, s );
If (n = 1)
{
Printf ("% d \ n", in [0]);
Continue;
}
Memset (son, 0, sizeof (son ));
For (I = 0; I <n; I ++) p [in [I] = I;
Ind = n-2;

... The remaining full text>

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.