Data structure and algorithm the non-recursive implementation of binary tree pre-order sequence is simplified

Source: Internet
Author: User

Node:

Class treenode{
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode (int val) {
        this.val = val;
    }
}

Pre-order:

Public list<integer> preordertraversal (TreeNode root) {
        list<integer> result = new Arraylist<> () ;
        TreeNode p = root;
        stack<treenode> stack = new stack<> ();
        while (P! = NULL | |!stack.isempty ()) {
            if (P! = null) {
                result.add (p.val);
                Stack.push (p);
                p = p.left;
            } else{
                p = stack.pop (). Right;
            }
        }
        return result;
    }

The idea is that as long as the Traverse pointer p is not empty, the output p, and then always look for its left, until null, then out of the stack, and then let P point to the right side of the stack, repeat the previous process, or always look for the left. Because the tree traversal we are generally DFS, deep traversal, deep traversal is the first to go deep. Here P is actually the root of the traversal of the subtree, the first root that is the first output p, and then put P into the stack, so that the right subtree can be recorded, and then the P is set to Zuozi the same processing. Only when the left dial hand tree is finished, the stack traverses the right subtree, still the same process.


Middle order:

    Public list<integer> inordertraversal (TreeNode root) {
        list<integer> result = new arraylist<> ();
        TreeNode p = root;
        stack<treenode> stack = new stack<> ();
        while (P! = NULL | |!stack.isempty ()) {
            if (P! = null) {
                stack.push (p);
                p = p.left;
            } else{
                TreeNode node = Stack.pop ();
                Result.add (node.val);
                p = node.right;
            }
        }
        return result;
    }
There is no difference between the basic and the preamble, only the position of the output root has changed. Only the left side is out of the stack output root.


Post

This is my concern this time, one interview has been asked. Today found a very good writing, quite concise, but also according to the previous ideas written.

After the post-traversal, the root position can be determined, is in the final position of the result queue, and then the root is Saozi right subtree, the difficulty lies in the left subtree root and right sub-tree root how to put. In general, we may think of putting the left subtree first, but the root of Zuozi does not find a place to put it. In fact, the right sub-tree better put, because the right sub-tree root position can be determined, is the parent node in front, that is, the second-to-last, this is a particularly useful law, directly decided that our program will be quite concise.

Then the logic of the program changes, we first put the root node in the end of the result, and then recursively handle the right subtree, in front of the root node to put the right subtree, and finally processing the left subtree, in front of the right subtree to put the left sub-tree. Why deal with the right subtree first? That was the discovery, because the root of the right subtree can be determined by the position, is the penultimate position.

It feels like a bunker. There are wood ...

    Public list<integer> postordertraversal (TreeNode root) {
        list<integer> result = new Arraylist<> ( );
        TreeNode p = root;
        stack<treenode> stack = new stack<> ();
        while (P! = NULL | |!stack.isempty ()) {
            if (P! = null) {
                stack.push (p);
                Result.add (0, p.val);
                p = p.right;
            } else{
                TreeNode node = Stack.pop ();
                p = node.left;
            }
        }
        return result;
    }


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.