104.二叉树的最大(小)深度
给定一个二叉树,找出其最大(小)深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例: 给定二叉树 [3,9,20,null,null,15,7],
返回它的最大(小)深度 3(2)
110.平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
返回 true 。
示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]
返回 false 。
问题描述
出现这中情况,一般是指针未初始化,指针越界。
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
//TreeNode(int val) :val(val), left(nullptr), right(nullptr) {}//
TreeNode(int val) :val(val){}//没有初始化指针
};
正确的完整代码如下:
// 二叉树的最大深度.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<cmath>
#include<vector>
#include<string>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
//TreeNode(int val) :val(val), left(nullptr), right(nullptr) {}//
TreeNode(int val) :val(val){}//没有初始化指针
};
class Solution {
public:
int count = 1;
int MostDeepofTreeerror(TreeNode* root) {
if (root == nullptr)return 0;
if (root->left != nullptr) {
count++;
MostDeepofTree(root->left);
}
if (root->right != nullptr) {
count++;
MostDeepofTree(root->right);
}
return count;
}
//二叉树深度最值
int MostDeepofTree(TreeNode* root) {
int leftheight = 0; //根据前序遍历,分别计算左边孩子的深度和右边孩子的深度,然后取最大(小)值
int rightheight = 0;
if (root == nullptr) return 0; //如果根(父)节点为空,直接返回0
if (root->left != nullptr) {
leftheight = MostDeepofTree(root->left); //递归调用
}
if (root->right != nullptr) {
rightheight = MostDeepofTree(root->right);
}
int result = leftheight < rightheight ? leftheight : rightheight;
int deep = result + 1; //
return deep;
}
bool isBalanceBinaryTree(TreeNode* root) {
int leftheight = 0;
int rightheight = 0;
if (root == nullptr) return 0;
if (root->left != nullptr) {
leftheight = MostDeepofTree(root->left);
}
if (root->right != nullptr) {
rightheight = MostDeepofTree(root->right);
}
int result = fabs(leftheight - rightheight);
if (result > 1) {
return false;
}
else {
return true;
}
}
/* int maxDeep(TreeNode* tree) {
return maxDeep(tree);
}*/
};
TreeNode* buildTree(vector<int>& nums) {
int n = nums.size();
if (n < 1) return nullptr;
vector<TreeNode*> nodes(n); // 用vector存储所有节点
// 初始化所有节点
for (int i = 0; i < n; i++) {
if (nums[i] != NULL) {
nodes[i] = new TreeNode(nums[i]);
}
}
// 为每个节点指定左右子节点的位置
int leftIndex = 1;
int rightIndex = 2;
for (int i = 0; i < n; i++) {
if (nodes[i] == nullptr) continue;
if (leftIndex < n && nodes[leftIndex] != nullptr) {
nodes[i]->left = nodes[leftIndex];
}
if (rightIndex < n && nodes[rightIndex] != nullptr) {
nodes[i]->right = nodes[rightIndex];
}
leftIndex += 2;
rightIndex += 2;
//cout << nodes[i]->val<<" 我爱我";
}
return nodes[0]; // 返回根节点指针
}
int main()
{
vector<int>num = { 1,2,3,4,5,6,7,8,9};
TreeNode* tree = buildTree(num);
Solution s;
//bool deep = s.isBalanceBinaryTree(tree);
int deep =s.MostDeepofTreeerror(tree);
cout << deep;
//cout <<boolalpha<< deep<<" "<< "我最棒";
return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
结果如下:
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
- 题目数据保证输入的树是 完全二叉树
问题描述:
下面这个是我自己编写的代码:
void LMF(TreeNode* cur, int count) {
if (cur != nullptr) count++;
if (cur->left != nullptr) {
LMF(cur->left, count++);
}
if (cur->right != nullptr) {
LMF(cur->right, count++);
}
cout << count<<"子函数输出";
}int NumTreeNode(TreeNode* tree) {
int bcount = 0;
LMF(tree, bcount);
return bcount;
}
这是测试以及运行结果:
我自己的思路就是前序遍历中,把原来存储元素的容器改为计数器
原因,void LMF(TreeNode* cur, int count) { 里的count需要加“&”,否则外部函数改变变量值不会影响LMF里面count的变化。
修正代码如下所示:
int bcount = 0;
void LMF(TreeNode* cur) {
if (cur != nullptr) bcount++;
if (cur->left != nullptr) {
LMF(cur->left);
}
if (cur->right != nullptr) {
LMF(cur->right);
}
//cout << bcount<<"子函数输出"<<endl;
}
int NumTreeNode(TreeNode* tree) {
LMF(tree);
// cout << "NumTreeNode子函数中" << bcount << endl;
return bcount;
}
引用测试例1,代码结果如下: