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

相关内容

热门资讯

父命难为是成语吗 父命难为是成语吗不是成语,意思是父亲的命令,不能违抗。这句话认为父亲生你养你是你的长辈,你是不可以说...
在路上送外卖时好多人对我指责,... 在路上送外卖时好多人对我指责,有时候甚至出声,如果我挂个音响放歌,是不是就不会有这种现象了?在路上送...
618是什么节日,为什么所有电... 618是什么节日,为什么所有电商都在促销?618是一个吉祥数字组成的,所谓大吉大利,店家以此为名促销...
我到底是谁?我来自哪里 我到底是谁?我来自哪里存在与未存在就在一刻之间,你存在就说明你脱离了未存在,有怎么知晓未存在的事呢,...
刀郎是什么人物,唱的好听的歌有... 刀郎是什么人物,唱的好听的歌有哪些?比较出名的就是2002年的第一场雪,其它披着羊皮的狼,爱是你我,...
求小说名,里面有用灵魂之力发挥... 求小说名,里面有用灵魂之力发挥符文的力量,符兽,祭祀星辰,大世界,三千小世界,等,?萧潜的秒杀。
骑马与砍杀战争之风新纪元雷神之... 骑马与砍杀战争之风新纪元雷神之锤近战霸气十足,当力量大于18点时基本秒杀顶级兵....就像那个仁兄说...
巴勒斯坦媒体:以军在加沙多地造... 新华财经加沙7月7日电(记者黄泽民)据巴勒斯坦通讯社7日报道,以色列军队当天在加沙地带多地继续军事行...
魔域秘药师德星化剂和记忆之眼怎... 魔域秘药师德星化剂和记忆之眼怎么得?除了在魔石商店..其他地方可以买吗?星化剂在魔石商店购买,你可以...
博来纳润为衢州集成电路产业延链... 转自:衢州日报  记者 胡灵萍  项目介绍:  衢州博来纳润电子材料有限公司年产18000吨纳米氧化...