代码随想录算法训练营Day14| 二叉树part01

代码随想录算法训练营Day14| 二叉树part01


文章目录

  • 代码随想录算法训练营Day14| 二叉树part01
  • 一、二叉树的递归遍历
    • 1.1前序遍历
    • 1.2中序遍历
    • 1.3 后序遍历
  • 二、二叉树的迭代遍历
    • 2.1 前序遍历
    • 2.2 中序遍历
    • 2.3 后序遍历
  • 三、统一迭代


二叉树遍历主要还是递归遍历,迭代遍历比较繁琐,对于我受挫迭代还是有些难度的

一、二叉树的递归遍历

1.1前序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        preOrder(root,result);
        return result;


    }

    //1.确定递归函数的参数和返回值
    public void preOrder(TreeNode root,List<Integer> result){
        //2.确定终止条件
        if(root==null){
            return;
        }
        //3.确定单层递归的逻辑
        result.add(root.val);
        preOrder(root.left,result);
        preOrder(root.right,result);
    }
}

1.2中序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {

    
    List<Integer> result=new ArrayList<>();
        inOrder(root,result);
        return result;
    }

    //1.确定递归函数的参数和返回值
    public void inOrder(TreeNode root,List<Integer> result){
        //2.确定终止条件
        if(root==null){
            return;
        }
        //3.确定单层递归的逻辑
       
        inOrder(root.left,result);
        result.add(root.val);
        inOrder(root.right,result);
         
    }
}

1.3 后序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
 List<Integer> result=new ArrayList<>();
        postOrder(root,result);
        return result;
    }

    //1.确定递归函数的参数和返回值
    public void postOrder(TreeNode root,List<Integer> result){
        //2.确定终止条件
        if(root==null){
            return;
        }
        //3.确定单层递归的逻辑
       
        postOrder(root.left,result);
        postOrder(root.right,result);
         result.add(root.val);
    }
}

二、二叉树的迭代遍历

2.1 前序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        if(root==null){
            return res;
        } 
        Stack<TreeNode> stack=new Stack<>();//临时栈
        //栈弹出的数是先入后出,所以要先存储右子树,再存储左子树
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node=stack.pop();
            //中
            res.add(node.val);
            //右
            if(node.right!=null){
                stack.push(node.right);
            }
            //左
            if(node.left!=null){
                stack.push(node.left);
            }
        }
        return res;
    }
}

2.2 中序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        if(root==null){
            return res;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;//cur表示当前的root,他是要移动的
        while(!stack.isEmpty() || cur!=null){
            //往左走,中序遍历要先往左走
            if(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            else{
                //如果没有左子树,那么就要上一个根节点的右子树开始遍历,所以要弹出上一个数
                cur=stack.pop();
                res.add(cur.val);
                cur=cur.right;
                
            }
        }
        return res;
    }
}

2.3 后序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res=new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<TreeNode> stack=new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node=stack.pop();
            //先把根节点先押入stack
            res.add(node.val);
            //再压入左节点
            if(node.left!=null){
                stack.push(node.left);
            }
           // 最后压入的右节点
            if(node.right!=null){
                stack.push(node.right);

            }
        }
        Collections.reverse(res);
        return res;
        
    }
}

三、统一迭代

这里只记录前序遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        Stack<TreeNode> st = new Stack<>();
        if (root != null) st.push(root);
        while (!st.empty()) {
            TreeNode node = st.peek();
            if (node != null) {
                st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中
                if (node.right!=null) st.push(node.right);  // 添加右节点(空节点不入栈)
                if (node.left!=null) st.push(node.left);    // 添加左节点(空节点不入栈)
                st.push(node);                          // 添加中节点
                st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
                
            } else { // 只有遇到空节点的时候,才将下一个节点放进结果集
                st.pop();           // 将空节点弹出
                node = st.peek();    // 重新取出栈中元素
                st.pop();
                result.add(node.val); // 加入到结果集
            }
        }
        return result;
    }
}

你可能感兴趣的:(代码随想录打卡,算法,数据结构)