代码随想录算法训练营第十五天| 层序遍历 226 翻转二叉树 101 对称二叉树

目录

层序遍历

102 二叉树的层序遍历

递归

迭代

107 二叉树的层序遍历 ||

 递归

迭代

199 二叉树的右视图

637 二叉树的层平均值

429 N叉树的层序遍历

515 在每个树行中寻找最大值

116 填充每个节点的下一个右侧节点指针

 117 填充每个节点的下一个右侧节点||

 104 二叉树的最大深度

 111 二叉树的最小深度

 226 翻转二叉树

递归

迭代

101 对称二叉树

递归

迭代


层序遍历

102 二叉树的层序遍历

递归

class Solution {
    List<List<Integer>> resList = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        int depth = 0;
        check(root,depth);
        return resList;
    }
    private void check(TreeNode root,Integer depth){
        if(root == null)return;
        depth++;
        if(resList.size() < depth){
            List<Integer> newList = new ArrayList<>(); 
            resList.add(newList);
        }
        resList.get(depth - 1).add(root.val);
        check(root.left,depth);
        check(root.right,depth);
    }
}

时间复杂度O(n)

空间复杂度O(h)h为二叉树的高度

迭代

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>>res = new ArrayList<List<Integer>>();
        if(root == null)return res;
        Deque<TreeNode>st = new LinkedList<>();
        st.add(root);
        while(!st.isEmpty()){
            List<Integer>newList = new ArrayList<>();
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                newList.add(cur.val);
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
            res.add(newList);
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)为栈所需空间

107 二叉树的层序遍历 ||

 递归

class Solution {
    List<List<Integer>>resList = new ArrayList<List<Integer>>();
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        int depth = 0;
        check(root,depth);
        List<List<Integer>>res = new ArrayList<List<Integer>>();
        for(int i = resList.size() - 1;i >= 0;i--){
            res.add(resList.get(i));
        }
        return res;
    }
    private void check(TreeNode root,Integer depth){
        if(root == null)return;
        depth++;
        if(resList.size() < depth){
            List<Integer>newList = new ArrayList<>();
            resList.add(newList);
        }
        resList.get(depth - 1).add(root.val);
        check(root.left,depth);
        check(root.right,depth);
    }
}

时间复杂度O(n)

空间复杂度O(n)  h为二叉树的高度,n为resList的大小,任何情况下都满足 h≤n,所以为O(n)

迭代

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>>res = new ArrayList<List<Integer>>();
        if(root == null)return res;
        Deque<TreeNode>st = new LinkedList();
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            List<Integer>newList = new ArrayList<>();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
                newList.add(cur.val);
            }
            res.add(newList);
        }
        List<List<Integer>>ares = new ArrayList<List<Integer>>();
        for(int i = res.size() - 1;i >= 0;i--){
            ares.add(res.get(i));
        }
        return ares;
    }
}

时间复杂度O(n)

空间复杂度O(n)

199 二叉树的右视图

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer>res = new ArrayList<>();
        Deque<TreeNode>st = new LinkedList<>();
        if(root == null)return res;
        st.addLast(root);
        while(!st.isEmpty()){
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                if(cur.left != null)st.addLast(cur.left);
                if(cur.right != null)st.addLast(cur.right);
                if(i == siz - 1)res.add(cur.val);
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)每个节点最多入栈一次

637 二叉树的层平均值

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double>res = new ArrayList<>();
        Deque<TreeNode> st = new LinkedList<>();
        if(root == null)return res;
        st.add(root);
        while(!st.isEmpty()){
            Double sum = 0.0;
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.poll();
                sum += cur.val;
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
            res.add(sum / siz);
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)  

429 N叉树的层序遍历

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>>res = new ArrayList<List<Integer>>();
        Deque<Node>st = new LinkedList<>();
        if(root == null)return res;
        st.add(root);
        while(!st.isEmpty()){
            List<Integer>newList = new ArrayList<>();
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                Node cur = st.poll();
                newList.add(cur.val);
                List<Node>childrens = cur.children;
                if(childrens == null || childrens.size() == 0)continue;
                for(Node node : childrens){
                    if(node != null)st.add(node);
                }
            }
            res.add(newList);
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)  即队列需要的空间大小,最坏情况下树为两层,第二层有n - 1个节点

515 在每个树行中寻找最大值

class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer>res = new ArrayList<Integer>();
        Deque<TreeNode>st = new LinkedList<>();
        if(root == null)return res;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            int mn = Integer.MIN_VALUE;
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                mn = Math.max(mn,cur.val);
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
            res.add(mn);
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

116 填充每个节点的下一个右侧节点指针

class Solution {
    public Node connect(Node root) {
        Deque<Node>st = new LinkedList<>();
        if(root == null)return null;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            Node cur = st.pollFirst();
            if(cur.left != null)st.add(cur.left);
            if(cur.right != null)st.add(cur.right);
            for(int i = 1;i < siz;i++){//cur已经取出,下标从1开始
                Node next = st.pollFirst();
                if(next.left != null)st.add(next.left);
                if(next.right != null) st.add(next.right);
                cur.next = next;
                cur = next;
            }
        }
        return root;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

 117 填充每个节点的下一个右侧节点||

class Solution {
    public Node connect(Node root) {
        Deque<Node>st = new LinkedList<>();
        if(root == null)return null;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            Node cur = st.pollFirst();
            if(cur.left != null)st.add(cur.left);
            if(cur.right != null)st.add(cur.right);
            for(int i = 1;i < siz;i++){
                Node next = st.pollFirst();
                if(next.left != null)st.add(next.left);
                if(next.right != null)st.add(next.right);
                cur.next = next;
                cur = next;
            }
        }
        return root;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

 104 二叉树的最大深度

class Solution {
    public int maxDepth(TreeNode root) {
        int depth = 0;
        Deque<TreeNode>st = new LinkedList<>();
        if(root == null)return depth;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
            depth++;
        }
        return depth;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

 111 二叉树的最小深度

class Solution {
    public int minDepth(TreeNode root) {
        int depth = 0;
        Deque<TreeNode>st = new LinkedList<>();
        if(root == null)return 0;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            depth++;
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                if(cur.left == null && cur.right == null)return depth;
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
        } 
        return -1;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

 226 翻转二叉树

递归

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null)return null;
        invertTree(root.left);
        invertTree(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}

时间复杂度O(n)

空间复杂度O(n) 使用的空间由递归栈的深度决定,最坏情况下二叉树为链状,此时为O(n)

迭代

class Solution {
    public TreeNode invertTree(TreeNode root) {
        Deque<TreeNode>st = new LinkedList<>();
        if(root == null)return null;
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pollFirst();
                TreeNode temp = cur.right;
                cur.right = cur.left;
                cur.left = temp;
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
        }
        return root;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

101 对称二叉树

递归

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left,root.right);
    }
    private boolean compare(TreeNode l,TreeNode r){
        if(l == null && r == null)return true;
        else if(l == null && r != null)return false;
        else if(l != null && r == null)return false;
        if(l.val != r.val)return false;
        boolean c1 = compare(l.left,r.right);
        boolean c2 = compare(l.right,r.left);
        return c1 && c2;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

迭代

class Solution {
    public boolean isSymmetric(TreeNode root) {
        Deque<TreeNode>deque = new LinkedList<>();
        deque.offerFirst(root.left);
        deque.offerLast(root.right);
        while(!deque.isEmpty()){
            TreeNode l = deque.pollFirst();
            TreeNode r = deque.pollLast();
            if(l == null && r == null)continue;
            if(l == null && r != null)return false;
            if(l != null && r == null)return false;
            if(l.val != r.val)return false;
            deque.offerFirst(l.left);
            deque.offerFirst(l.right);
            deque.offerLast(r.right);
            deque.offerLast(r.left);
        }
        return true;
    }
}

时间复杂度O(n)

空间复杂度O(n) 

  • 78
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值