算法练习 DAY16 || 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树的最小深度 ● 222.完全二叉树的节点个数

104.二叉树的最大深度

在这里插入图片描述

递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

而根节点的高度就是二叉树的最大深度
在这里插入图片描述

后序

//递归 后序(求高度)
class Solution1 {
public:
	//确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,
	//最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)
	//就是目前节点为根节点的树的深度。
	int getdepth(TreeNode* cur) {
		if (cur == nullptr) return 0;
		int leftdepth = getdepth(cur->left);
		int rightdepth = getdepth(cur->right);
		int maxdepth = max(leftdepth, rightdepth) + 1;
		return maxdepth;
	}
	int maxdepth(TreeNode* root) {
		return getdepth(root);
	}
};

前序

class Solution2 {
public:
	int result;
	void getdepth(TreeNode* cur , int depth) {
		result = result > depth ? result : depth; //中
		if (cur->left == nullptr && cur->right == nullptr) return;
		if (cur->left) { //左
			depth++;
			getdepth(cur->left, depth);
			depth--;
		} 
		if (cur->right) { //右
			depth++;
			getdepth(cur->right, depth);
			depth--;
		}
		return;
	}
	int maxdepth(TreeNode* root) {
		result = 0;
		if (root == nullptr) return result;
		getdepth(root, 1);
		return result;
	}
};

559.n叉树的最大深度

递归法

// Definition for a Node.
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 == nullptr) return 0;
        int depth = 0;
        for(int i = 0; i<root->children.size();i++){
            depth = max(depth,maxDepth(root->children[i]));
        }
        return depth+1;
    }
};

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
什么是叶子节点,左右孩子都为空的节点才是叶子节点!

示例 1:
说明: 叶子节点是指没有子节点的节点。
输入:root = [3,9,20,null,null,15,7]
输出:2

示例 2:
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
在这里插入图片描述求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

递归(后序)
class Solution {
public:
	int getDepth(TreeNode* node) {
		if (node == nullptr) return 0;
		int leftdepth = getDepth(node->left);
		int rightdepth = getDepth(node->right);
		// 当一个左子树为空,右不为空,这时并不是最低点
		if (node->left == nullptr && node->right != nullptr) {
			return rightdepth + 1;
		}
		// 当一个右子树为空,左不为空,这时并不是最低点
		if (node->left != nullptr && node->right == nullptr) {
			return leftdepth + 1;
		}
		return min(leftdepth, rightdepth) + 1;
	}
	int minDepth(TreeNode* root) {
		return getDepth(root);
	}
};
递归(前序)
class Solution {
public:
	int result;
	void getDepth(TreeNode* node, int depth) {
		if (node->left == nullptr&&node->right == nullptr) {
			result = min(result, depth); //中
			return;
		}
		if (node->left != nullptr) getDepth(node->left, depth + 1);
		if (node->right != nullptr) getDepth(node->right, depth + 1);

		return;
	}
	int minDepth(TreeNode* root) {
		if (root == nullptr) return 0;
		result = INT_MAX;
		getDepth(root, 1);
		return result;
	}
}

222.完全二叉树的节点个数

给出一个完全二叉树,求出该树的节点个数。

示例 1:

输入:root = [1,2,3,4,5,6]
输出:6

示例 2:

输入:root = []
输出:0

示例 3:

输入:root = [1]
输出:1

提示:

树中节点的数目范围是[0, 5 * 10^4]
0 <= Node.val <= 5 * 10^4
题目数据保证输入的树是 完全二叉树

思路:
完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
在这里插入图片描述

这里关键在于如果去判断一个左子树或者右子树是不是满二叉树呢?

在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树
在这里插入图片描述完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图:
在这里插入图片描述

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、付费专栏及课程。

余额充值