110.平衡二叉树
题目链接:110. Balanced Binary Tree - 力扣(LeetCode)
题目描述:给定一个二叉树,判断它是否是平衡二叉树
平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:true
示例 2:
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例 3:
输入:root = []
输出:true
提示:
- 树中的节点数在范围
[0, 5000]
内 -104 <= Node.val <= 104
思路
求子树的高度,可以通过求该子树根结点的最大深度再加一来获得
解法一、递归
int getDepth(struct TreeNode* node) {
//如果结点不存在,返回0
if(!node)
return 0;
//求出右子树深度
int rightDepth = getDepth(node->right);
//求出左子树深度
int leftDepth = getDepth(node->left);
//返回左右子树中的较大值+1
return rightDepth > leftDepth ? rightDepth + 1 : leftDepth + 1;
}
bool isBalanced(struct TreeNode* root) {
//递归结束条件为:传入结点为NULL,返回True
if(!root)
return 1;
//求出左右子树的深度
int leftDepth = getDepth(root->left);
int rightDepth = getDepth(root->right);
//若左右子树绝对值差距大于1,返回False
if(abs( leftDepth - rightDepth) > 1)
return 0;
//检查左右子树是否为平衡二叉树
return isBalanced(root->right) && isBalanced(root->left);
}
解法二、迭代
//计算结点深度
int getDepth(struct TreeNode* node) {
//开辟栈空间
struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10000);
int stackTop = 0;
//若传入结点存在,将其入栈。若不存在,函数直接返回0
if(node)
stack[stackTop++] = node;
int result = 0;
int depth = 0;
//当栈中有元素时,进行迭代遍历
while(stackTop) {
//取出栈顶元素
struct TreeNode* tempNode = stack[--stackTop];
//若栈顶元素非NULL,则将深度+1
if(tempNode) {
depth++;
//将栈顶元素再次入栈,添加NULL表示此结点已被遍历
stack[stackTop++] = tempNode;
stack[stackTop++] = NULL;
//若栈顶元素有左右孩子,则将孩子结点入栈
if(tempNode->left)
stack[stackTop++] = tempNode->left;
if(tempNode->right)
stack[stackTop++] = tempNode->right;
//更新结果
result = result > depth ? result : depth;
}
else {
//若为NULL,则代表当前结点已被遍历,深度-1
tempNode = stack[--stackTop];
depth--;
}
}
return result;
}
bool isBalanced(struct TreeNode* root){
//开辟栈空间
struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10000);
int stackTop = 0;
//若根节点不存在,返回True
if(!root)
return 1;
//将根节点入栈
stack[stackTop++] = root;
//当栈中有元素时,进行遍历
while(stackTop) {
//将栈顶元素出栈
struct TreeNode* node = stack[--stackTop];
//计算左右子树的深度
int diff = getDepth(node->right) - getDepth(node->left);
//若深度的绝对值大于1,返回False
if(diff > 1 || diff < -1)
return 0;
//如果栈顶结点有左右结点,将左右结点入栈
if(node->left)
stack[stackTop++] = node->left;
if(node->right)
stack[stackTop++] = node->right;
}
//若二叉树遍历结束后没有返回False,则返回True
return 1;
}
257.二叉树的所有路径
题目链接:257. 二叉树的所有路径 - 力扣(LeetCode)
题目描述:给你一个二叉树的根节点 root
,按 任意顺序 ,返回所有从根节点到叶子节点的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [1,2,3,null,5]
输出:["1->2->5","1->3"]
示例 2:
输入:root = [1]
输出:["1"]
提示:
- 树中节点的数目在范围
[1, 100]
内 -100 <= Node.val <= 100
思路
本题求的是从根节点到叶子节点的路径,所以选择前序遍历,方便让父节点指向孩子节点,找到对应的路径
解法一、递归–深度优先搜索
void dfs(struct TreeNode* root, char** path, int* returnSize, int* stk,
int top) {
if (root) {
if (root->left == NULL && root->right == NULL) {
char* tmp = (char*)malloc(sizeof(char) * 1001);
tmp[0] = '\0';
for (int i = 0; i < top; i++) {
sprintf(tmp + strlen(tmp), "%d->", stk[i]);
}
sprintf(tmp + strlen(tmp), "%d", root->val);
path[(*returnSize)++] = tmp;
} else {
stk[top++] = root->val;
dfs(root->left, path, returnSize, stk, top);
dfs(root->right, path, returnSize, stk, top);
}
}
}
char** binaryTreePaths(struct TreeNode* root, int* returnSize) {
char** path = (char**)malloc(sizeof(char*) * 1001);
*returnSize = 0;
int stk[1001];
dfs(root, path, returnSize, stk, 0);
return path;
}
解法二、迭代
404.左叶子之和
题目链接:404. 左叶子之和 - 力扣(LeetCode)
题目描述:给定二叉树的根节点 root
,返回所有左叶子之和。
示例 1:
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
示例 2:
输入: root = [1]
输出: 0
提示:
- 节点数在
[1, 1000]
范围内 -1000 <= Node.val <= 1000
解法一、递归
int sumOfLeftLeaves(struct TreeNode* root){
// 递归结束条件:若当前结点为空,返回0
if(!root)
return 0;
// 递归取左子树的左结点和和右子树的左结点和
int leftValue = sumOfLeftLeaves(root->left);
int rightValue = sumOfLeftLeaves(root->right);
// 若当前结点的左结点存在,且其为叶子结点。取它的值
int midValue = 0;
if(root->left && (!root->left->left && !root->left->right))
midValue = root->left->val;
return leftValue + rightValue + midValue;
}
解法二、迭代
int sumOfLeftLeaves(struct TreeNode* root){
struct TreeNode* stack[1000];
int stackTop = 0;
// 若传入root结点不为空,将其入栈
if(root)
stack[stackTop++] = root;
int sum = 0;
//若栈不为空,进行循环
while(stackTop) {
// 出栈栈顶元素
struct TreeNode *topNode = stack[--stackTop];
// 若栈顶元素的左孩子为左叶子结点,将其值加入sum中
if(topNode->left && (!topNode->left->left && !topNode->left->right))
sum += topNode->left->val;
// 若当前栈顶结点有左右孩子。将他们加入栈中进行遍历
if(topNode->right)
stack[stackTop++] = topNode->right;
if(topNode->left)
stack[stackTop++] = topNode->left;
}
return sum;
}
222.完全二叉树的结点个数
题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)
题目描述:给你一棵 完全二叉树 的根节点 root
,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h
层,则该层包含 1~ 2h
个节点。
示例 1:
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1
提示:
- 树中节点的数目范围是
[0, 5 * 104]
0 <= Node.val <= 5 * 104
- 题目数据保证输入的树是 完全二叉树
解法一、递归
int countNodes(struct TreeNode* root) {
if(root == NULL) return 0;
return countNodes(root->left)+countNodes(root->right)+1;
}
解法二、迭代–层序遍历
int countNodes(struct TreeNode* root){
//记录结点总数
int totalNum = 0;
//开辟栈空间
struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 100);
int stackTop = 0;
//如果root结点不为NULL,则将其入栈。若为NULL,则不会进入遍历,返回0
if(root)
stack[stackTop++] = root;
//若栈中有结点存在,则进行遍历
while(stackTop) {
//取出栈顶元素
struct TreeNode* tempNode = stack[--stackTop];
//结点总数+1
totalNum++;
//若栈顶结点有左右孩子,将它们入栈
if(tempNode->left)
stack[stackTop++] = tempNode->left;
if(tempNode->right)
stack[stackTop++] = tempNode->right;
}
return totalNum;
}