java数据结构之二叉树(1)

二叉树的前中后序遍历-递归

在这里插入图片描述

递归 前中后序 遍历二叉树

/**
 * 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> res = new ArrayList<Integer>();
        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);
        //res.add(root.val); 中序遍历 
        preorder(root.right, res);
        res.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> preorderTraversal(TreeNode root) {
    List<Integer> res = new ArrayList<>();
    if(root == null){
        return res;
    }
    Deque<TreeNode> stack = new LinkedList<>();
    TreeNode node = root;
    while(!stack.isEmpty() || node!=null){
        while(node!=null){
            res.add(node.val);
            stack.push(node);
            node = node.left;
        }
        node=stack.pop();
        node=node.right;
    }
    return 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> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root==null){
            return res;
        }
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode node = root;
        while(!stack.isEmpty()||node!=null){
            while(node!=null){
                stack.push(node);
                node=node.left;
            }
            node=stack.pop();
            res.add(node.val);
            node=node.right;
        }
        return 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> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();

        if(root == null){
            return res;
        }
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode prev = null;
        while(stack.isEmpty()|| root!=null){
            while(root!=null){
                stack.push(root);
                root=root.left;
            }
            root = stack.pop();
            if(root.right == null || root.right == prev ){
                res.add(root.val);
                prev = root;
                root == null;

            }else{
                stack.push(root);
                root=root.right;
                
            }
        }
        return 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<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }

        Deque<TreeNode> stack = new LinkedList<>();
        stack.offer(root);
        while(!stack.isEmpty()){
            List<Integer> ans = new ArrayList<>();
            int cur = stack.size();
            for(int i =0;i<cur;i++){
                TreeNode node = stack.pop();
                ans.add(node.val);
                if(node.left!=null){
                    stack.offer(node.left);
                }
                if(node.right!=null){
                    stack.offer(node.right);
                }
            }
            res.add(ans);
        }
        return 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> rightSideView(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<>();
        if(root!=null) stack.push(root);
        List<Integer> res = new ArrayList<>();
        
        while(!stack.isEmpty()){

            int cur = stack.size();

            for(int i = 0;i<cur;i++){

                TreeNode node = stack.pop();

                if(i==cur-1) res.add(node.val);//核心公式

                if(node.left!=null) stack.addLast(node.left);

                if(node.right!=null) stack.addLast(node.right);
            }
        }

        return 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> rightSideView(TreeNode root) {
        // Deque<TreeNode> stack = new LinkedList<>();
        // if(root!=null) stack.push(root);
        // List<Integer> res = new ArrayList<>();
        
        // while(!stack.isEmpty()){

        //     int cur = stack.size();

        //     for(int i = 0;i<cur;i++){

        //         TreeNode node = stack.pop();

        //         if(i==cur-1) res.add(node.val);

        //         if(node.left!=null) stack.addLast(node.left);

        //         if(node.right!=null) stack.addLast(node.right);
        //     }
        // }

        // return res;

        //深度优先遍历
          Map<Integer,Integer> map = new HashMap<>();
          int max_depth = -1;
          Deque<TreeNode> nodeStack = new LinkedList<>();
          Deque<Integer> depthStack = new LinkedList<>();

          nodeStack.push(root);
          depthStack.push(0);

          while(!nodeStack.isEmpty()){
              TreeNode node = nodeStack.pop();
               int depth = depthStack.pop();

               if(node !=null){
                   max_depth = Math.max(max_depth,depth);

                   if(!map.containsKey(depth)){
                       map.put(depth,node.val);
                   }

                   nodeStack.push(node.left);
                   nodeStack.push(node.right);
                   depthStack.push(depth+1);
                   depthStack.push(depth+1);
               }
          }
          List<Integer> res = new ArrayList<>();
          for(int i = 0;i<=max_depth;i++){
              res.add(map.get(i));
          }
          return 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> rightSideView(TreeNode root) {
        // Deque<TreeNode> stack = new LinkedList<>();
        // if(root!=null) stack.push(root);
        // List<Integer> res = new ArrayList<>();
        
        // while(!stack.isEmpty()){

        //     int cur = stack.size();

        //     for(int i = 0;i<cur;i++){

        //         TreeNode node = stack.pop();

        //         if(i==cur-1) res.add(node.val);

        //         if(node.left!=null) stack.addLast(node.left);

        //         if(node.right!=null) stack.addLast(node.right);
        //     }
        // }

        // return res;

        //深度优先遍历
          Map<Integer,Integer> map = new HashMap<>();
          int max_depth = -1;
          Queue<TreeNode> nodeStack = new LinkedList<>();
          Queue<Integer> depthStack = new LinkedList<>();

          nodeStack.add(root);
          depthStack.add(0);

          while(!nodeStack.isEmpty()){
              TreeNode node = nodeStack.remove();
               int depth = depthStack.remove();

               if(node !=null){
                   max_depth = Math.max(max_depth,depth);


                   map.put(depth,node.val);

                   nodeStack.add(node.left);
                   nodeStack.add(node.right);
                   depthStack.add(depth+1);
                   depthStack.add(depth+1);
               }
          }
          List<Integer> res = new ArrayList<>();
          for(int i = 0;i<=max_depth;i++){
              res.add(map.get(i));
          }
          return 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<Double> averageOfLevels(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<>();
        if(root!=null) stack.push(root);
        List<Double> res = new ArrayList<>();
        
        while(!stack.isEmpty()){

            int cur = stack.size();
            double sum = 0;

            for(int i = 0;i<cur;i++){

                TreeNode node = stack.pop();

                sum+=node.val;//核心公式

                if(node.left!=null) stack.addLast(node.left);

                if(node.right!=null) stack.addLast(node.right);
            }
            res.add(sum/cur);
        }

        return res;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值