Iterative and recursive traversal of a binary tree traversal

Binary tree preorder traversal (recursive version):

public ArrayList<Integer> inOrder(TreeNode root ){
        ArrayList<Integer> result = new ArrayList<Integer>();
        if(root == null){
             return result;
        }
        result.add(root.val);
        inOrder(root.left);+
        inOrder(root.right);
        return result;
    }    

 

Binary tree in order traversal (recursive version):

 

public ArrayList<Integer> inOrder(TreeNode root ){
        ArrayList<Integer> result = new ArrayList<Integer>();
        if(root == null){
             return result;
        }
        inOrder(root.left);
        result.add(root.val);
        inOrder(root.right);
        return result;
    }

Binary Tree subsequent traversal (recursive version):

public ArrayList<Integer> inOrder(TreeNode root ){
        ArrayList<Integer> result = new ArrayList<Integer>();
        if(root == null){
             return result;
        }
        inOrder(root.left);
        inOrder(root.right);
        result.add(root.val);
        return result;
    }

Recursive version Summary:

In fact, that is where you add a node different

Preorder traversal: adding a node is added before the recursive;

Inorder traversal: Adding nodes around the node between the recursion;

Postorder: a node is added after about recursive node;

 

Binary tree preorder traversal (iterative version):

 
 
public ArrayList<Integer> preOrder(TreeNode root){
ArrayList<Integer> result= new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
if(root == null){
return result;
}
if(root != null){
stack.push(root);
result.add(root.val);
root=root.left;
}else{
root=stack.pop();
root=root.right;
}
return result;
}
 

Binary tree traversal sequence (iterative version):

public ArrayList<Integer> inOrder(TreeNode root){
        ArrayList<Integer> result = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        if(root == null){
             return result;
        }
        while(root != null || !stack.isEmpty()){
            if(root != null){
                stack.push(root);
                root=root.left;
            }else{
                root= stack.pop();
                result.add(root.val);
                root= root.right;
            }
        }
        return result;
    }

 

After traversing Binary Tree (iterative version):

The most difficult he began to think that, when two before you understand, you will find him in front of order traversal like!

    public ArrayList<Integer> postTreeNode(TreeNode root){
        Stack<TreeNode> midstack = new Stack<TreeNode>();
        Stack<TreeNode> resultstack= new Stack<TreeNode>() ;
        ArrayList<Integer> result = new ArrayList<Integer>();
        while(root != null || !midstack.isEmpty()){
            if(root != null){
                midstack.push(root);
                resultstack.push(root);
                root=root.right;
            }else{
                root=midstack.pop();
                root=root.left;
            }
        }
        while(resultstack.size() > 0){
            TreeNode temp = resultstack.pop();
            result.add(temp.val);
        }
        return result;
    }

 

 

Guess you like

Origin www.cnblogs.com/frank9571/p/11963441.html