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;
}
}