mgaw

导航

代码随想录算法训练营第十六天| 104 二叉树的最大深度 111 二叉树的最小深度 222 完全二叉树的节点个数

目录

104 二叉树的最大深度

迭代 

递归

 111 二叉树的最小深度

迭代 

递归

 222 完全二叉树的节点个数

迭代 

递归

利用完全二叉树性质


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)

递归

class Solution {
    public int maxDepth(TreeNode root) {
        return getDepth(root);
    }
    private int getDepth(TreeNode root){
        if(root == null)return 0;
        int ldepth = getDepth(root.left);
        int rdepth = getDepth(root.right);
        return Math.max(ldepth,rdepth) + 1;
    }
}

时间复杂度O(n)

空间复杂度O(h)h为递归栈空间大小,即为树的深度

迭代 

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)

递归

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)return 0;//终止条件
        if(root.left == null && root.right == null)return 1;
        int res = Integer.MAX_VALUE;
        if(root.left != null){
            res = Math.min(res,minDepth(root.left));
        }
        if(root.right != null){
            res = Math.min(res,minDepth(root.right));
        }
        return res + 1;
    }
}

时间复杂度O(n)

空间复杂度O(h)

迭代 

class Solution {
    public int countNodes(TreeNode root) {
        Deque<TreeNode>st = new LinkedList<>();
        int cnt = 0;
        if(root == null)return cnt;
        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);
                cnt++;
            }
        }
        return cnt;
    }
}

时间复杂度O(n)

空间复杂度O(n)

递归

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null)return 0;
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

时间复杂度O(n)

空间复杂度O(logn)

利用完全二叉树性质

class Solution {
    public int countNodes(TreeNode root) {
        int res = 0;
        if(root == null)return res;
        int ldepth = getDepth(root.left);
        int rdepth = getDepth(root.right);
        if(ldepth == rdepth){
            //左子树的深度等于右子树的深度,则左子树为满二叉树
            //节点数等于左子树的节点 + 右子树的节点 + 根节点
            //1 << n 相当于对1进行左移n次的操作,即为2的n次方
            return (1 << ldepth) - 1 + countNodes(root.right) + 1;//<<符号优先级较低,需要加()
        }
        //如果左子树的深度大于右子树,则右子树为满二叉树
        //节点数 = 左子树的节点 + 右子树的节点 + 根节点
        return (1 << rdepth) - 1 + countNodes(root.left) + 1;
        
    }
    private int getDepth(TreeNode root){
        int depth = 0;
        while(root != null){
            depth++;
            root = root.left;
        }
        return depth;
    }
}

时间复杂度O(n)

空间复杂度O(logn×logn)while的空间复杂度乘递归的时间复杂度

posted on 2023-11-09 10:40  A魔法恐龙  阅读(7)  评论(0)    收藏  举报  来源