Binary tree traversal [learning algorithm]

Preface

2023-8-7 15:13:50

The following content is derived from "[Creative Template Five]"
and is for learning and communication purposes only.

copyright

Delete the following words when publishing on other platforms.
This article was first published on the CSDN platform.
The author is CSDN@日星月云.
The homepage of the blog is https://blog.csdn.net/qq_51625007.
Delete the above words when publishing on other platforms.

recommend

Chapter 6 Tree [Data Structure and Algorithm]

Binary tree traversal [learning algorithm]

preorder traversal

144. Preorder traversal of binary tree

Recursive implementation

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            list.add(root.val);
            preorderTraversal(root.left,list);
            preorderTraversal(root.right,list);      
        }
    }
}

Recursive non-implementation -1

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    
            while(cur!=null){
    
    
                list.add(cur.val);
                stack.push(cur);
                cur=cur.left;
            }
            if(!stack.isEmpty()){
    
    
                cur=stack.pop();
                cur=cur.right;
            }
        }
    }
}

Recursive non-implementation-2

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    
            if(cur!=null){
    
    
                list.add(cur.val);
                stack.push(cur);
                cur=cur.left;
            }else{
    
    
                cur=stack.pop();
                cur=cur.right;
            }
        }
    }
}

inorder traversal

94. In-order traversal of binary tree

Recursive implementation

class Solution {
    
    
    //递归
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        inorderTraversal(root,res);
        return res;
    }
    public void inorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            inorderTraversal(root.left,list);
            list.add(root.val);
            inorderTraversal(root.right,list);
        }
    
    }

}

Non-recursive implementation-1

class Solution {
    
    
   
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    //当前结点指针及栈均空,则结束
            while(cur!=null){
    
    //访问根结点,根指针进栈,进入左子树
                stack.push(cur);
                cur=cur.left;
            }
            if(!stack.isEmpty()){
    
    
                cur=stack.pop();
                list.add(cur.val);
                cur=cur.right;//根指针退栈,进入其右子树
            }
        }

        return list;
    }
   


}

Non-recursive implementation-2

class Solution {
    
    
    //递归
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        inorderTraversal(root,res);
        return res;
    }
    public void inorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        //结点不为空或栈不为空
        while(cur!=null||!stack.isEmpty()){
    
    
            //结点不为空
            if(cur!=null){
    
    
                stack.push(cur);
                cur=cur.left;
            }else{
    
    
                cur=stack.pop();
                list.add(cur.val);
                cur=cur.right;
            }
        }
    
    }

}

Postorder traversal

recursion

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        postorderTraversal(root,res);
        return res;
    }
    public void postorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            postorderTraversal(root.left,list);
            postorderTraversal(root.right,list);
            list.add(root.val);
        }
        
    }
}

non-recursive

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root,next=null;
        while(cur!=null||!stack.isEmpty()){
    
    
            //先把结点给全进栈
            while(cur!=null){
    
    
                stack.push(cur);
                cur=cur.left;//左
            }
            //栈不为空
            if(!stack.isEmpty()){
    
    
                cur=stack.peek();
                if(cur.right==null||cur.right==next){
    
    
                    //判断栈顶结点的右子树是否为空,或者刚被访问过
                    cur=stack.pop();
                    res.add(cur.val);//根
                    next=cur;//记录刚访问
                    cur=null;//清空
                }else{
    
    
                    cur=cur.right;//右
                }
            }
        }


        return res;
    }

}

at last

2023-8-7 16:00:45

We all have a bright future

I wish you all the best in your postgraduate entrance exams, I wish you all success
in your work, I wish you all get what you wish for, like, collect and follow.


Guess you like

Origin blog.csdn.net/qq_51625007/article/details/132146961