自学内容网 自学内容网

代码随想录算法训练营Day11

144.二叉树的前序遍历

递归法

/**
 * 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<>();
        preorder(root,res);
        return res;
    }
    public  void preorder(TreeNode root,List<Integer> res){
        if(root==null){
            return ;
        }
        res.add(root.val);
        preorder(root.left,res);
        preorder(root.right,res);
    }
}

迭代法

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

    }
}

94.二叉树的中序遍历

递归法

/**
 * 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<>();
        midorder(root,result);
        return result;
    }
    public void midorder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        midorder(root.left,result);
        result.add(root.val);
        midorder(root.right,result);
    }
}

迭代法

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

145.二叉树的后序遍历

递归法

/**
 * 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<>();
        afterorder(root,result);
        return result;

    }
    public void afterorder(TreeNode root,List<Integer> result){
        if(root==null){
            return;
        }
        afterorder(root.left,result);
        afterorder(root.right,result);
        result.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) {
        List<Integer> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        Stack<TreeNode> mystack=new Stack<>();
        mystack.push(root);
        while(!mystack.isEmpty()){
            TreeNode cur=mystack.pop();
            result.add(cur.val);
            if(cur.left!=null){
                mystack.push(cur.left);
            }
            if(cur.right!=null){
                mystack.push(cur.right);
            }
        }
        Collections.reverse(result);
        return result;

    }
}

102.二叉树的层序遍历

/**
 * 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<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result=new ArrayList<List<Integer>>();
        lo(root,result);
        return result;
    }
    public void lo(TreeNode root,List<List<Integer>> result){
       if(root==null)
       return;
        Deque<TreeNode> myque=new LinkedList<>();
        myque.offer(root);
        while(!myque.isEmpty()){
            List<Integer> curlist=new ArrayList<>();
            int len=myque.size();
            while(len>0){
            TreeNode cur=myque.poll();
            curlist.add(cur.val);
            len--;
            if(cur.left!=null){
                myque.offer(cur.left);
            }
            if(cur.right!=null){
                myque.offer(cur.right);
            }
            } 
            result.add(curlist);      
        }
    }
}


原文地址:https://blog.csdn.net/lynyzy/article/details/142460297

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!