代码随想录第16天:104.二叉树的最大深度 559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

文章讲述了如何使用递归和迭代方法来计算二叉树和N叉树的最大深度、最小深度以及完全二叉树的节点数量。递归法基于后序遍历和先序遍历,而迭代法则采用层序遍历。对于最小深度,处理了空节点和非空节点的不同情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

104:二叉树最大深度

1、递归法:

利用后序遍历求最大深度。

1)确定递归函数参数和返回值:每次就传入一个结点,返回当前结点的最大深度;

2)递归结束条件:当结点值为空的时候,返回0;

3)单层递归逻辑:当前结点不为空,求出左节点的最大深度和右节点的最大深度,返回左节点和右节点中的最大值+1(+1是因为要考虑当前结点)

class Solution {
    public int maxDepth(TreeNode root) {
    	if (root == null) {
			return 0;
		}
		int leftHeight = maxDepth(root.left);
		int rightHeught = maxDepth(root.right);
		return leftHeight>rightHeught?leftHeight+1:rightHeught+1;
    }
}

2、迭代法:使用层序遍历

class Solution {
    public int maxDepth(TreeNode root) {
        int depth = 0;
		if (root == null) {
			return 0;
		}
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			int len = queue.size();
			for (int i = 0; i < len; i++) {
				TreeNode node = queue.poll();
				if (node.left != null) {
					queue.offer(node.left);
				}
				if (node.right != null) {
					queue.offer(node.right);
				}
			}
			depth++;
		}
		return depth;
    }
}

549:N叉树的最大深度

递归法:

class Solution {
    public int maxDepth(Node root) {
        if (root==null) {
			return 0;
		}
        int depth = 0;
        for (Node node : root.children) {
			depth = Math.max(maxDepth(node), depth);
		}
        return depth+1;    
    }
}

迭代法:层序遍历

class Solution {
    public int maxDepth(Node root) {
        if (root==null) {
			return 0;
		}
        int depth = 0;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
			int len = queue.size();
			for (int i = 0; i < len; i++) {
				Node node = queue.poll();
				for (Node n : node.children) {
					if (n!=null) {
						queue.offer(n);
					}
				}
			}
			depth++;
		}
        return depth;
    }
}

111:二叉树的最小深度

1、递归法:

1)确定递归函数参数和返回值:传入参数就是一个结点,返回值为最小深度。

2)递归终止条件:当当前结点为null的时候,最小深度为0

3)单层递归逻辑:当前传入的结点不为空的时候,求出左子结点的最小深度和右子节点的最小深度。但是与求最大深度不一样的地方在于,不能直接返回二者的最小值。如果左结点为空,右结点不为空,最小深度应该为右节点的最小深度+1,如果右结点为空,左结点不为空,最小深度应该为左节点的最小深度+1。如果左右结点都为空,才返回二者的最小值。

class Solution {
    public int minDepth(TreeNode root) {
    	if (root==null) {
			return 0;
		}
		int leftMin = minDepth(root.left);
		int rightMin = minDepth(root.right);
		if (root.left == null && root.right!=null) {
			return 1+rightMin;
		}
		if (root.left != null && root.right==null) {
			return 1+leftMin;
		}
		return Math.min(leftMin, rightMin)+1;
    }
}

2、迭代法:层序遍历

class Solution {
    public int minDepth(TreeNode root) {
    	int depth = 0;
		if (root == null) {
			return 0;
		}
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			int len = queue.size();
			for (int i = 0; i < len; i++) {
				TreeNode node = queue.poll();
				if (node.left != null) {
					queue.offer(node.left);
				}
				if (node.right != null) {
					queue.offer(node.right);
				}
				if ((node.left==null)&&(node.right==null)) {
					return depth+1;
				}
			}
			depth++;
		}
		return depth;
    }
}

222:完全二叉树的结点数量

1、递归法(按照普通二叉树):

1)确定递归函数参数和返回值:传入参数就是一个结点,返回值为结点数目。

2)递归终止条件:当当前结点为null的时候,结点数目为0;

3)单层递归逻辑:返回当前节点的左子树的节点数量和右子树的节点数量之和+1

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

2、递归法(按照完全二叉树):

1)确定递归函数参数和返回值:传入参数就是一个结点,返回值为结点数目。

2)递归终止条件:当当前结点为null的时候,结点数目为0;因为是完全二叉树,所以如果子树是满二叉树的话,就不用继续遍历,直接计算节点数目为2^h-1,其中h为二叉树的深度。

3)单层递归逻辑:返回当前节点的左子树的节点数量和右子树的节点数量之和+1

class Solution {
    public int countNodes(TreeNode root) {
    	if (root==null) {
			return 0;
		}
		TreeNode lefTreeNode = root.left;
		TreeNode righTreeNode = root.right;
		int leftDepth=0,rightDepth=0;
		while (lefTreeNode!=null) {
			lefTreeNode = lefTreeNode.left;
			leftDepth++;
		}
		while (righTreeNode!=null) {
			righTreeNode = righTreeNode.right;
			rightDepth++;
		}
		if (leftDepth == rightDepth) {
			return (2<<leftDepth)-1;
		}
		int left = countNodes(root.left);
		int right = countNodes(root.right);
		return left+right+1;
    }
}

3、层序遍历:依次计算每个层的节点数量

class Solution {
    public int countNodes(TreeNode root) {
        if (root==null) {
			return 0;
		}
		int count = 0;
		Queue<TreeNode> queue = new LinkedList<>();
		queue.offer(root);
		while (!queue.isEmpty()) {
			int len = queue.size();
			count += len;
			for (int i = 0; i < len; i++) {
				TreeNode node = queue.poll();
				if (node.left!=null) {
					queue.offer(node.left);
				}
				if (node.right!=null) {
					queue.offer(node.right);
				}
			}
		}
		return count;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值