My Thirtieth Page - 遍历讲解 - By Nicolas

这篇page我们主要讲解遍历的方法和条件。遍历有前序遍历、中序遍历、以及后续遍历还有层序遍历,小尼这边给出遍历的方法有递归法和迭代法小尼一次给出。

前序遍历(递归):

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> arr = new ArrayList<>();
        qianxubianli(root,arr);
        return arr;
    }
    void qianxubianli(TreeNode root,List<Integer> List){
        if(root == null){
            return;
        }
        List.add(root.val);
        qianxubianli(root.left,List);
        qianxubianli(root.right,List);
    }
}

前序遍历(迭代):

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;
    }
}

中序遍历(递归):

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

中序遍历(迭代):

class Solution {
    public List<Integer> inorderTraversal(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);
                st.push(node);
                st.push(null);
                if(node.left != null) st.push(node.left);
            }else{
                st.pop();
                node = st.peek();
                st.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

后续遍历(递归):

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

后续遍历(迭代):

class Solution {
    public List<Integer> postorderTraversal(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();
                st.push(node);
                st.push(null);
                if(node.right != null) st.push(node.right);
                if(node.left != null) st.push(node.left);
            }else{
                st.pop();
                node = st.peek();
                st.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

层序遍历:(迭代)

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

    public List<List<Integer>> levelOrder(TreeNode root) {
        check(root);
        return resList;
    }
    public void check(TreeNode root){
        if(root == null) return;
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(root);

        while(!que.isEmpty()){
            List<Integer> itList = new ArrayList<Integer>();
            int len = que.size();

            while(len > 0){
                TreeNode temNode = que.poll();
                itList.add(temNode.val);

                if(temNode.left!=null) que.offer(temNode.left);
                if(temNode.right!=null) que.offer(temNode.right);
                len--;
            }
            resList.add(itList);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值