随想录一刷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);}
};

相关内容

热门资讯

东方航空×泡泡玛特:DIMOO... 文旅观察近日,中国东方航空与泡泡玛特携手推出的DIMOO中泰建交50周年限定主题彩绘专机正式首航,执...
多个“第一”!重大突破! 今天(7月9日)上午国新办举行首场“高质量完成‘十四五’规划”系列主题新闻发布会一起看 ↓国家发展改...
中广核新能源:6月发电量140... 格隆汇7月9日|中广核新能源(1811.HK)公告,6月本集团按合并报表口径完成发电量1,403.5...
1.52亿千瓦!江苏电网最高用... 转自:上观新闻7月7日,江苏电网最高用电负荷,年内第三次刷新历史新高,达1.52亿千瓦。截至目前,南...
ST华铭因信披违规被罚,收购聚... 近日,ST华铭(维权)(300462)发布公告,近日收到中国证券监督管理委员会上海监管局下发的《行政...
山西泽辰医药乌帕替尼缓释片启动... 药物临床试验登记与信息公示平台数据显示,山西泽辰医药科技有限公司的乌帕替尼缓释片在健康受试者中随机、...
西海都市报公布地址、订阅及零售... 本报地址:西宁市长江路5号 广告经营许可证: 广告部电话:6125601 排版:西海都市报社编辑部 ...
台风丹娜丝“绘就”陆家嘴诗意画... 转自:上观新闻连日来,受台风“丹娜丝”外围环流持续影响,上海晴雨相间,同时带来降温、大风,云雾光影间...
国家发改委:中国做的这件事,全... 转自:北京日报客户端在国新办今天(9日)举行的首场“高质量完成‘十四五’规划”系列主题新闻发布会上,...
或受欧盟法规影响,苹果将在欧盟...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! IT之家 7 月 9...