代码随想录第14天|二叉树的递归遍历、迭代遍历、统一迭代

二叉树的递归遍历

/**
 * 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> ret = new ArrayList<>();
        //前序遍历
        preorder(root, ret);
        //后序遍历
        postorder(root, ret);
        //中序遍历
        inorder(root, ret);
        return ret;
    }
        //前序遍历
    public void preorder(TreeNode root, List<Integer> ret){
        if(root == null){
            return;
        }
        ret.add(root.val);
        preorder(root.left, ret);
        preorder(root.right, ret);
    }
        //后序遍历
    public void postorder(TreeNode root, List<Integer> ret){
        if(root == null){
            return;
        }
        postorder(root.left, ret);
        postorder(root.right, ret);
        ret.add(root.val);
    }
        //中序遍历
    public void inorder(TreeNode root, List<Integer> ret){
        if(root == null){
            return;
        }
        inorder(root.left, ret);
        ret.add(root.val);
        inorder(root.right, ret);
    }
}

二叉树的迭代遍历

/**
 * 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> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Stack<TreeNode> st = new Stack<>();
        st.push(root);
        while(!st.isEmpty()){
            TreeNode tmp = st.pop();
            ret.add(tmp.val);
            if(tmp.right != null){
                st.push(tmp.right);
            }
            if(tmp.left != null){
                st.push(tmp.left);
            }
        }
        return ret;
    }
}

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        //前序:中左右
        //后序:左右中
        //后序迭代:压栈时先左后右-中右左-翻转后左右中
        List<Integer> ret = new ArrayList<>();
        Stack<TreeNode> st = new Stack<>();
        if(root == null){
            return ret;
        }
        st.push(root);
        while(!st.isEmpty()){
            TreeNode tmp = st.pop();
            ret.add(tmp.val);
            if(tmp.left != null){
                st.push(tmp.left);
            }
            if(tmp.right != null){
                st.push(tmp.right);
            }
        }
        Collections.reverse(ret);
        return ret;
    }
}

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        //中序遍历--迭代法
        List<Integer> ret = new ArrayList<>();
        if(root == null){
            return ret;
        }
        Stack<TreeNode> st = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !st.isEmpty()){
            if(cur != null){
                st.push(cur);
                cur = cur.left;
            }else{//cur为空,叶子节点
                cur = st.pop();
                ret.add(cur.val);
                cur = cur.right;
            }
        }
        return ret;
    }
}
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        //中序遍历--统一迭代法
        List<Integer> ret = new ArrayList<>();
        if(root != null){
            return ret;
        }
        Stack<TreeNode> st = new Stack<>();
        st.push(root);
        while(!st.isEmpty()){
            TreeNode node = st.peek();
            if(node != null){
                st.pop();//右中左顺序入栈,左中右出栈
                if(node.right != null){
                    st.push(node.right);
                }
                st.push(node);
                st.push(null);
                if(node.left != null){
                    st.push(left);
                }
            }else{
                st.pop();
                node = st.peek();
                st.pop();
                ret.add(node.val);
            }
        }
        return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值