数据结构-树的前序,中序,后序,层序遍历(java)

中序遍历(非递归实现)

public void traversal(TreeNode node){
    Stack stack = new Stack();
    while(node != null||!stack.empty()){
        while(node != null){
            stack.push(node);
            node = node.left;
        }
        if(!stack.empty()){
            node = stack.pop();
            System.out.println(node.val);
            node = node.right;
        }
    }
}

前序遍历(非递归实现)根、左、右

Stack stack = new Stack();
stack.push(root);
while(stack.size() != 0){
    root = stack.pop();
    System.out.println(root.val);
    if(root.right != null){
        stack.push(root.right);
    }
    if(root.left != null){
        stack.push(root.left);
    }
}

后序遍历(非递归)左、右、根

public List<Integer> postorderTraversal(TreeNode root) {
    List<Integer> result = new ArrayList<>();
    if(root == null){
        return result;
    }
    Stack<TreeNode> stack = new Stack<>();
    stack.push(root);
    TreeNode pre = null;
    while(stack.size() > 0){
        root = stack.peek();
        if(root.left == null&&root.right == null||(pre != null&&(root.left == pre||root.right == pre))){
            result.add(root.val);
            pre = stack.pop();
        }else{
            if(root.right != null){
                stack.add(root.right);
            }
            if(root.left != null){
                stack.add(root.left);
            }
        }
    }
    return result;
}

层序遍历(非递归)

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for(int i = 0;i < size;i++){
                TreeNode node = queue.remove();
                list.add(node.val);
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
            }
            res.add(list);
        }
        return res;
    }

层序遍历(递归)

class Solution {
    List<List<Integer>> levels = new ArrayList<List<Integer>>();

    public void helper(TreeNode node, int level) {
        // start the current level
        if (levels.size() == level)
            levels.add(new ArrayList<Integer>());

         // fulfil the current level
         levels.get(level).add(node.val);

         // process child nodes for the next level
         if (node.left != null)
            helper(node.left, level + 1);
         if (node.right != null)
            helper(node.right, level + 1);
    }
    
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) return levels;
        helper(root, 0);
        return levels;
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值