前言
思路及算法思维,指路 代码随想录。
题目来自 LeetCode。
day 17,又是一个令人愉快的周五~
题目详情
[110] 平衡二叉树
题目描述
110 平衡二叉树
解题思路
前提:平衡二叉树:左右子树高度差不超过1,
思路:从平衡二叉树定义上,可以看出判断平衡二叉树的方法是后序遍历各个结点高度差。
重点:平衡二叉树的判断。
代码实现
C语言
后序遍历计算高度, 递归
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int maxFun(int a, int b)
{return (a > b) ? a : b;
}int absFun(int a, int b)
{return (a > b) ? (a - b) : (b - a);
}int countHight(struct TreeNode *root)
{if (root == NULL){return 0;}// 计算左右子树高度int leftHight = countHight(root->left);if (leftHight < 0){return -1;}int rightHight = countHight(root->right);if (rightHight < 0){return -1;}// 判断左右子树是否为平衡二叉树if (abs(leftHight - rightHight) > 1){return -1;}return 1 + maxFun(leftHight, rightHight);
}bool isBalanced(struct TreeNode* root) {return (countHight(root) < 0) ? false : true;
}
[257] 二叉树的所有路径
题目描述
257 二叉树的所有路径
解题思路
前提:二叉树的路径,就是从根节点到叶子结点的路径
思路:前序遍历
重点:递归回溯过程中的结点路径的保存。
代码实现
C语言
先序遍历 递归回溯 + sprintf
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/
/*** Note: The returned array must be malloced, assume caller calls free().*/// 先序遍历
void travesal(struct TreeNode *root, char **result, int *returnSize, int *nums, int idx)
{if (root == NULL){return ;}// 中nums[idx++] = root->val;if ((root->left == NULL) && (root->right == NULL)){// 遍历到叶子结点,输出路径(*returnSize)++;char *path = (char *)malloc(sizeof(char) * 1001);int len = 0;for (int i = 0; i < idx; i++){if (i != (idx - 1)){len += sprintf(path + len, "%d->", nums[i]);}else{len += sprintf(path + len, "%d", nums[i]);}}result[(*returnSize) - 1] = path;}else{// 未遍历到有叶子结点,继续向子节点遍历// 左travesal(root->left, result, returnSize, nums, idx);// 右travesal(root->right, result, returnSize, nums, idx);}return ;
}char** binaryTreePaths(struct TreeNode* root, int* returnSize) {*returnSize = 0;char **result = (char **)malloc(sizeof(char *) * 1001);int nums[1001];int idx = 0;travesal(root, result, returnSize, nums, idx);return result;
}
[404] 左叶子之和
题目描述
404 左叶子之和
解题思路
前提:所求为左叶子结点的值的和
思路:遍历结点,找到所有叶子结点,且为左结点。
重点:左叶子结点的父节点,可能是右结点。
代码实现
C语言
先序,递归:标识左右结点
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int travesal(struct TreeNode *node, bool isLeft)
{int leftSum = 0;if (node == NULL){return 0;}// 叶子结点的判断if ((node->left == NULL) && (node->right == NULL)){if (isLeft == true){leftSum = node->val;}}// 非叶子结点// 左leftSum += travesal(node->left, true);// 右leftSum += travesal(node->right, false);return leftSum;
}int sumOfLeftLeaves(struct TreeNode* root){return travesal(root, false);
}
递归: 通过父节点判断是否为左叶子结点
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/int travesal(struct TreeNode *node)
{int leftSum = 0;int rightSum = 0;if ((node == NULL) || ((node->left == NULL) && (node->right == NULL))){return 0;}// 判断是否为左叶子结点,通过父节点实现判断if ((node->left != NULL) && (node->left->left == NULL) && (node->left->right == NULL)){leftSum = node->left->val;}else{// 非左叶子结点//左leftSum = travesal(node->left);}//右rightSum = travesal(node->right);return leftSum + rightSum;
}int sumOfLeftLeaves(struct TreeNode* root){return travesal(root);
}
今日收获
- 平衡二叉树的判断;
- 二叉树路径的输出。