代码随想录刷题第16天|二叉树 LeetCode104二叉树的最大深度、LeetCode559n叉树的最大深度、LeetCode111二叉树的最小深度、LeetCode222完全二叉树的节点个数

1、LeetCode104 二叉树的最大深度

题目链接:104、二叉树的最大深度

首先看高度和深度的概念:

方法一:后序遍历(左右中)求根节点的高度

根节点的高度就是二叉树的最大深度。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int leftHeight = maxDepth(root->left);          //左
        int rightHeight = maxDepth(root->right);        //右
        int depth = 1 + max(leftHeight, rightHeight);   //中
        return depth;
    }
};

方法二:层序遍历

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int depth = 0;
        while(!que.empty())
        {
            int size = que.size();
            depth++;
            while(size--)
            {
                TreeNode* Node = que.front();
                que.pop();
                if (Node->left) que.push(Node->left);
                if (Node->right) que.push(Node->right);
            }
        }
        return depth;
    }
};

方法三:前序遍历(中左右),体验一下回溯法

class Solution {
public:
    int result;
    void getdepth(TreeNode * Node, int depth)
    {
        result = depth > result ? depth : result;         //中

        if (Node->left == NULL && Node->right == NULL) return;

        if (Node->left)                     // 左
        {
            depth++;                        // 深度+1
            getdepth(Node->left, depth);
            depth--;                        // 回溯,深度-1
        }
        if (Node->right)                    // 右
        {
            depth++;                        // 深度+1
            getdepth(Node->right, depth);
            depth--;                        // 回溯,深度-1
        }
        return;
    }

    int maxDepth(TreeNode* root) {
        result = 0;
        if (root == NULL) return 0;
        getdepth(root, 1);
        return result;

    }
};

2、LeetCode559 n叉树的最大深度

题目链接:559、n叉树的最大深度

n叉树的构造函数:

class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};

递归法:

class Solution {
public:
    int maxDepth(Node* root) {
        if (root == NULL) return 0;
        int depth = 0;
        for (int i = 0; i < root->children.size(); i++)
        {
            depth = max( depth, maxDepth(root->children[i]) );
        }   
        return depth + 1;
    }
};

迭代法(层序遍历):

class Solution {
public:
    int maxDepth(Node* root) {
        if (root == NULL) return 0;
        queue<Node*> que;
        que.push(root);
        int depth = 0;
        while(!que.empty())
        {
            int size = que.size();
            depth++;
            while(size--)
            {
                Node* node = que.front();
                que.pop();
                for (int i = 0; i < node->children.size(); i++)
                {
                    if (node->children[i]) que.push(node->children[i]);
                }
            }           
        }
        return depth;
    }
};

3、LeetCode111 二叉树的最小深度

题目链接:111、二叉树的最小深度

与求最大深度类似,这里返回 1 + min( leftHeight, rightHeight )

但需要注意的是,以下情况返回的是1,但叶子结点是左右孩子都为空的节点,应该是4这个节点,返回值应该是3。需要增加判断条件。

 

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int leftHeight = minDepth(root->left);
        int rightHeight = minDepth(root->right);

        if (root->left == NULL && root->right != NULL)
        {
            return 1 + rightHeight;
        }
        else if (root->left != NULL && root->right == NULL)
        {
            return 1 + leftHeight;
        }
        else
        {
            return 1 + min(leftHeight, rightHeight);
        }
    }
};

层序遍历法:

当从队列头部弹出元素时,判断如果该元素的左右孩子都为空,说明该节点是叶子结点,立马返回depth,depth不再++;

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        queue<TreeNode*> que;
        int depth = 0;
        que.push(root);
        while(!que.empty())
        {
            int size = que.size();
            depth++;
            while(size--)
            {
                TreeNode * Node = que.front();
                que.pop();
                if (Node->left) que.push(Node->left);
                if (Node->right) que.push(Node->right);

                if (Node->left == NULL && Node->right ==NULL)
                {
                    return depth;
                }
            }
        }
        return depth;

    }
};

4、LeetCode222 完全二叉树的节点个数

题目链接:222、完全二叉树节点个数

普通二叉树节点个数:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == NULL) return 0;  
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

层序遍历法:

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result++;   // 记录节点数量
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

完全二叉树:

在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值