随想录一刷Day18——二叉树
创始人
2024-04-07 12:10:30
0

文章目录

  • Day18_二叉树
    • 16. 找树左下角的值
    • 17. 路径总和
    • 18. 从中序与后序遍历序列构造二叉树

Day18_二叉树

16. 找树左下角的值

513. 找树左下角的值
思路1:递归法(前序遍历)

记录遍历到的最大深度,记录该深度下第一个遍历到的元素值,不断更新结果至遍历完所有的结点,即找到最大值。

class Solution {
public:int MaxDepth = -1;int result = INT_MAX;void tranversal(TreeNode* root, int Depth) {if (root->left == NULL && root->right == NULL) {if (Depth > MaxDepth) { // 大于是为了保证最深层的result是最左结点的值MaxDepth = Depth;result = root->val;}return ;}if (root->left) tranversal(root->left, Depth + 1);if (root->right) tranversal(root->right, Depth + 1);return ;}int findBottomLeftValue(TreeNode* root) {// 题目要求是至少有一个结点,即不存在第一节点为空的情况tranversal(root, 0);return result; }
};

思路2:迭代法(层序遍历)

层序遍历记录每层的第一个元素,答案为最后一层的第一个元素

class Solution {
public:int findBottomLeftValue(TreeNode* root) {// 题目要求是至少有一个结点,即不存在第一节点为空的情况int result;queue que;que.push(root);while (!que.empty()) {int que_size = que.size();for (int i = 0; i < que_size; i++) {TreeNode* node = que.front();que.pop();if (i == 0) result = node->val;if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return result;}
};

17. 路径总和

112. 路径总和
思路:

先序遍历,在叶子节点处判断路径和是否满足条件

class Solution {
public:bool ans_flag = false;void tranversal(TreeNode* root, int sum) {if (ans_flag) return ;if (root->left == NULL && root->right == NULL) { // 到叶子节点再判断是否满足if (sum == 0) {ans_flag = true;}return ;}if (root->left) tranversal(root->left, sum - root->left->val);if (root->right) tranversal(root->right, sum - root->right->val);}bool hasPathSum(TreeNode* root, int targetSum) {if (root == NULL) return false;tranversal(root, targetSum - root->val);return ans_flag;}
};

精简递归的写法

class Solution {
public:bool hasPathSum(TreeNode* root, int targetSum) {if (root == NULL) return false;if (!root->left && !root->right && targetSum == root->val) return true;return hasPathSum(root->left, targetSum - root->val) | hasPathSum(root->right, targetSum - root->val);}
};

利用栈模拟递归回溯

class Solution {
public: // 先序遍历bool hasPathSum(TreeNode* root, int targetSum) {if (root == NULL) return false;stack> stk;stk.push(pair(root, root->val));while (!stk.empty()) {pair node = stk.top();stk.pop();if (!node.first->left && !node.first->right && node.second == targetSum) return true; // 中if (node.first->right) stk.push({node.first->right, node.second + node.first->right->val}); // 右if (node.first->left) stk.push({node.first->left, node.second + node.first->left->val}); // 左}return false;}
};

18. 从中序与后序遍历序列构造二叉树

106. 从中序与后序遍历序列构造二叉树
思路:

根据中序和后续的遍历顺序特点,互相配合构造出二叉树
后续遍历 左 右 中,最后一个元素作为当前子树的根节点
中序遍历 左 中 右,子树的根结点左边为左子树值,右边为右子树值
反复递归划分区间,直到区间大小全部划分为1,回溯构造二叉树

class Solution {
private:TreeNode* tranversal(vector inorder, vector postorder) {if (postorder.size() == 0) return NULL;// 后序遍历数组中的最后一个元素作为当前的子树的根int root_value = postorder[postorder.size() - 1];TreeNode* root = new TreeNode(root_value);// 左右递归至只有一个元素,子树的建立完成if (postorder.size() == 1) return root;// 找到中序遍历的切割点int divide_index;for (int i = 0; i < inorder.size(); i++) {if (inorder[i] == root_value) {divide_index = i;break;}}// 切割中序遍历数组,左闭右开vector leftInorder(inorder.begin(), inorder.begin() + divide_index);vector rightInorder(inorder.begin() + divide_index + 1, inorder.end());// posterorder 舍弃末尾元素postorder.resize(postorder.size() - 1);// 切割后续数组,左闭右开,由于遍历顺序导致中序和后续前一部分都是左子树的遍历结果,所以后续和中序划分方式相同vector leftPosterorder(postorder.begin(), postorder.begin() + divide_index);vector rightPosterorder(postorder.begin() + divide_index, postorder.end());root->left = tranversal(leftInorder, leftPosterorder);root->right = tranversal(rightInorder, rightPosterorder);return root;}public:TreeNode* buildTree(vector& inorder, vector& postorder) {if (inorder.size() == 0) return NULL;return tranversal(inorder, postorder);}
};

相关内容

热门资讯

孩子从小到大习惯性晨起就排便,... 孩子从小到大习惯性晨起就排便,他的肠胃好不好呢?说明他的肠道很好啊。如果每天都能够按时排便。说明他的...
爱到极度疯狂,爱到无法想象,爱... 爱到极度疯狂,爱到无法想象,爱到像狂风吹下落叶,,,爱到没了自我就是爱的极限。爱真的有那么大的力量吗...
mersin是哪个港口 mersin是哪个港口mersin是梅尔辛。土耳其南部最大港口,伊切尔省省会,与泉州市是友好城市。位...
纳指收跌0.9% 日、韩公司股... 转自:财联社【纳指收跌0.9% 日、韩公司股票走低】财联社7月8日电,美股三大指数集体收跌,道指跌0...
成语日渐消瘦什么意思? 成语日渐消瘦什么意思?日渐消瘦不是成语,它的意思是每日看上去都比前一日要虚弱一点,形容人的身体或精神...
体验青年生活新风尚 转自:团结报  7月3日,“湾Day in北京”2025京台青年生活节在北京卫星制造厂科技园开幕。 ...
临夏州:旅游大通道串起好风景 新甘肃·甘肃日报记者 王虎 夏日的太子山,绿意漫山峦,野花缀草间。长286公里的临夏州沿太子山旅游大...
中老铁路累计发送旅客逾5500... 转自:云南日报中老铁路累计发送旅客逾5500万人次吸引115个国家和地区的旅客跨境旅行记者从中国铁路...
张掖进京推介文化旅游资源   本报讯(记者 颉亚珍)“这是裕固族的民族服装,裕固族只在甘肃有。欢迎来我们肃南裕固族自治县旅游,...
台湾政党团体纪念全民族抗战爆发... 转自:团结报  据新华社消息  为纪念全民族抗战爆发88周年、抗日战争胜利和台湾光复80周年,台湾劳...