二叉树先中后序遍历(递归非递归)

先序遍历

先序遍历也叫先根遍历,采用先访问根节点,再访问左节点,最后访问右节点

递归实现
/**
 * 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>list=new LinkedList<Integer>();
        bl(root,list);
        return list;
    }
    public void bl(TreeNode root,List<Integer> list){
        if(root==null){
            return;
        }
        list.add(root.val);
        bl(root.left,list);
        bl(root.right,list);
    }


}
非递归实现

非递归实现借助了栈

/**
 * 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) {
        return xhbl(root);
    }
    public List<Integer> xhbl(TreeNode root){
        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();

        while(root!=null || !stack.isEmpty()){
            
            while (root!=null){
                list.add(root.val);
                stack.push(root.right);
                root=root.left;
            }

            root=stack.pop();
            //root=root.right;
        }
        return list;
    }


}

中序遍历

中序遍历的顺序为 左节点,根节点、右节点

递归实现
/**
 * 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> list=new LinkedList<Integer>();
        accTre(root,list);
        return list;
    }

    public void accTre(TreeNode res,List<Integer> list){
        if(res==null){
            return ;
        }
        accTre(res.left,list);
        list.add(res.val);
        accTre(res.right,list);
    }
}
非递归实现

当然非递归实现也是采用了栈

/**
 * 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) {
        return xhbl(root);
    }

   public List<Integer> xhbl(TreeNode root){
        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();

        while(root!=null || !stack.isEmpty()){
            while (root!=null){
                stack.push(root);
                root=root.left;
            }
            
            root=stack.pop();
            list.add(root.val);
            root=root.right;
        }
        return list;
    }
}

后序遍历

后续遍历的顺序是左节点、右节点、根节点

递归实现
/**
 * 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> list=new LinkedList<Integer>();
        hxbl(root,list);
        return list;
    }

    public void hxbl(TreeNode root,List<Integer> list){
        if(root==null){
            return;
        }

        hxbl(root.left,list);
        hxbl(root.right,list);
        list.add(root.val);

    }

}
非递归实现

非递归实现采用栈和临时变量,
用临时变量标识当前节点的右子节点是否被访问过,从而确定是否将当前节点输出

/**
 * 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) {
        
        return xhbl(root);
    }

    public List<Integer> xhbl(TreeNode root){

        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode press = null;

        while(root!=null || !stack.isEmpty()){
            while (root!=null){
                stack.push(root);
                root=root.left;
            }

            root=stack.pop();
            if(root.right==null || root.right==press){
                list.add(root.val);
                press=root;
                root=null;
            }else {
                stack.push(root);
                root=root.right;
            }
        }
        return list;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值