104.二叉树的最大深度
思路:本题无亮点,主要是有递归法(三部曲——>精简)和迭代法(层次遍历...)
// 递归法(三部曲)
int getDepth(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
int depth = max(leftDepth, rightDepth) + 1;
return depth;
}
int maxDepth1(TreeNode* root) {
int res = getDepth(root);
return res;
}
// 递归法(精简版)
int maxDepth2(TreeNode* root) {
return root == NULL ? 0 : max(maxDepth2(root->left), maxDepth2(root->right)) + 1;
}
// 前序遍历+递归(体现深度回溯)
int result;
void getDepth1(TreeNode* node, int depth) {
result = depth > result ? depth : result;// 根
if (node->left == NULL && node->right == NULL) {
return;
}
// 左
if (node->left) {
// 深度+1
depth++;
// 递归
getDepth1(node->left, depth);
// 回溯,深度-1
depth--;
}
// 右
if (node->right) {
// 深度+1
depth++;
getDepth1(node->right, depth);
// 回溯,深度-1
depth--;
}
return;
}
// 精简版
void getDepth11(TreeNode* node, int depth) {
result = depth > result ? depth : result;
if (node->left == NULL && node->right == NULL) {
return;
}
if (node->left) {
getDepth11(node->left, depth + 1);
}
if (node->right) {
getDepth11(node->right, depth + 1);
}
return ;
}
int maxDepth3(TreeNode* root) {
result = 0;
if (root == NULL) {
return result;
}
getDepth1(root, 1);
return result;
}
// 层次遍历+迭代法
int maxDepth4(TreeNode* root) {
if (root == NULL) {
return 0;
}
int depth = 0 ;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
// size记录que大小,否则会变动
int size = que.size();
// while循环:一次一层
++depth;
// 每次遍历一整层的节点
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
}
return depth;
}
559.n叉树的最大深度
思路:注意n叉数的子树为一个数组
// 递归法
int maxDepth(Node* root) {
if (root == 0) {
return 0;
}
int depth = 0;
for (int i = 0; i < root->children.size(); ++i) {
depth = max(depth, maxDepth(root->children[i]));
}
return depth + 1;
}
// 层序遍历+迭代法
int maxDepth1(Node* root) {
queue<Node*> que;
if (root != NULL) {
que.push(root);
}
int depth = 0;
while (!que.empty()) {
int size = que.size();
depth++;
for (int i = 0; i < size; ++i) {
Node* node = que.front();
que.pop();
for (int j = 0; j < node->children.size(); ++j) {
if (node->children[j]) {
que.push(node->children[j]);
}
}
}
}
return depth;
}
111.二叉树的最小深度
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 ,注意是叶子节点。
什么是叶子节点,左右孩子都为空的节点才是叶子节点!
// 如果节点的左右孩子为空,则说明他是叶子节点(当前分支的最底层)
// 层序遍历+迭代法
int minDepth1(TreeNode* root) {
if (root == NULL) {
return 0;
}
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
depth++;
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
else if (node->left == NULL && node->right == NULL) {
return depth;
}
}
}
return depth;
}
// 递归法
int minDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (root->left == NULL && root->right != NULL) {
return minDepth(root->right) + 1;
}
if (root->right == NULL && root->left != NULL) {
return minDepth(root->left) + 1;
}
return min(minDepth(root->left), minDepth(root->right)) + 1;
}
222.完全二叉树的节点个数
可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量。
这里关键在于如何去判断一个左子树或者右子树是不是满二叉树呢?
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。
在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树.
// 递归法
int count(TreeNode* node) {
int res = 0;
if (node == NULL) {
return res;
}
if (node->left) {
++res;
res += count(node->left);
}
if (node->right) {
++res;
res += count(node->right);
}
return res;
}
int countNodes1(TreeNode* root) {
if (root == NULL) {
return 0;
}
return 1 + count(root);
}
// 层次遍历+迭代法
int countNodes2(TreeNode* root) {
int count = 0;
if (root == NULL) {
return count;
}
// 创建辅助队列
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
count++;
if (node->left) {
que.push(node->left);
}
if (node->right) {
que.push(node->right);
}
}
}
return count;
}
// 完全二叉树特性
// 时间复杂度:O(log n × log n)
// 空间复杂度:O(log n)
int countNodes(TreeNode* root) {
if (root == nullptr) {
return 0;
}
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0;
int rightDepth = 0;
// 求左子树深度
while (left) {
left = left->left;
leftDepth++;
}
// 求右子树深度
while (right) {
right = right->right;
rightDepth++;
}
if (leftDepth == rightDepth) {
return (2 << leftDepth) - 1;
}
return countNodes(root->left) + countNodes(root->right) + 1;
}