深圳幻海软件技术有限公司 欢迎您!

C/C++每日一练(20230326) 二叉树专场(3)

2023-04-12

目录1.二叉树的前序遍历  🌟🌟2.二叉树的最大深度  🌟3.有序数组转换为二叉搜索树  🌟🌟🌟每日一练刷题专栏 🌟Golang每日一练专栏Python每日一练专栏C/C++每日一练专栏Java每日一练专栏1.二叉树

目录

1. 二叉树的前序遍历  🌟🌟

2. 二叉树的最大深度  🌟

3. 有序数组转换为二叉搜索树  🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

示例 4:

输入:root = [1,2]
输出:[1,2]

示例 5:

输入:root = [1,null,2]
输出:[1,2]

提示:

  • 树中节点数目在范围 [0, 100] 内
  • -100 <= Node.val <= 100

进阶:递归算法很简单,你可以通过迭代算法完成吗?

出处:

https://edu.csdn.net/practice/23819517

代码: 递归算法

  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. using namespace std;
  5. struct TreeNode
  6. {
  7. int val;
  8. TreeNode *left, *right;
  9. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  10. };
  11. class Solution
  12. {
  13. private:
  14. void rec(TreeNode *root, vector<int> &ret)
  15. {
  16. if (root != nullptr)
  17. {
  18. ret.push_back(root->val);
  19. rec(root->right, ret);
  20. rec(root->left, ret);
  21. }
  22. }
  23. public:
  24. vector<int> postorderTraversal(TreeNode *root)
  25. {
  26. vector<int> ret;
  27. rec(root, ret);
  28. return ret;
  29. }
  30. };
  31. string Vector2String(vector<int> vect) {
  32. stringstream ss;
  33. ss << "[";
  34. for (size_t i = 0; i < vect.size(); i++)
  35. {
  36. ss << to_string(vect[i]);
  37. ss << (i < vect.size() - 1 ? "," : "]");
  38. }
  39. return ss.str();
  40. }
  41. int main()
  42. {
  43. TreeNode *root = new TreeNode(1);
  44. root->right = new TreeNode(2);
  45. root->right->left = new TreeNode(3);
  46. Solution s;
  47. cout << Vector2String(s.postorderTraversal(root)) << endl;
  48. return 0;
  49. }

输出:

[2,1,3]

进阶: 迭代算法

  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <queue>
  5. #include <stack>
  6. #define null INT_MIN
  7. using namespace std;
  8. struct TreeNode
  9. {
  10. int val;
  11. TreeNode *left, *right;
  12. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  13. };
  14. TreeNode* buildTree(vector<int>& nums)
  15. {
  16. if (nums.empty()) return nullptr;
  17. TreeNode *root = new TreeNode(nums.front());
  18. queue<TreeNode*> q;
  19. q.push(root);
  20. int i = 1;
  21. while(!q.empty() && i < nums.size())
  22. {
  23. TreeNode *cur = q.front();
  24. q.pop();
  25. if(i < nums.size() && nums[i] != null)
  26. {
  27. cur->left = new TreeNode(nums[i]);
  28. q.push(cur->left);
  29. }
  30. i++;
  31. if(i < nums.size() && nums[i] != null)
  32. {
  33. cur->right = new TreeNode(nums[i]);
  34. q.push(cur->right);
  35. }
  36. i++;
  37. }
  38. return root;
  39. }
  40. void preorderPrint(TreeNode* root) {
  41. stack<TreeNode*> st;
  42. st.push(root);
  43. while (!st.empty()) {
  44. TreeNode* node = st.top();
  45. st.pop();
  46. if (node != nullptr) {
  47. cout << node->val << " ";
  48. st.push(node->right);
  49. st.push(node->left);
  50. }
  51. }
  52. cout << endl;
  53. }
  54. void preorderPrint2(TreeNode* root) {
  55. stack<TreeNode*> st;
  56. TreeNode* node = root;
  57. while (node != nullptr || !st.empty()) {
  58. while (node != nullptr) {
  59. cout << node->val << " ";
  60. st.push(node);
  61. node = node->left;
  62. }
  63. node = st.top();
  64. st.pop();
  65. node = node->right;
  66. }
  67. cout << endl;
  68. }
  69. vector<int> preorderTraversal(TreeNode* root) {
  70. vector<int> res;
  71. stack<TreeNode*> st;
  72. st.push(root);
  73. while (!st.empty()) {
  74. TreeNode* node = st.top();
  75. st.pop();
  76. if (node != nullptr) {
  77. res.push_back(node->val);
  78. st.push(node->right);
  79. st.push(node->left);
  80. }
  81. }
  82. return res;
  83. }
  84. vector<int> preorderTraversal2(TreeNode* root) {
  85. vector<int> res;
  86. stack<TreeNode*> st;
  87. TreeNode* node = root;
  88. while (node != nullptr || !st.empty()) {
  89. while (node != nullptr) {
  90. res.push_back(node->val);
  91. st.push(node);
  92. node = node->left;
  93. }
  94. node = st.top();
  95. st.pop();
  96. node = node->right;
  97. }
  98. return res;
  99. }
  100. string vectorToString(vector<int> vect) {
  101. stringstream ss;
  102. ss << "[";
  103. for (size_t i = 0; i < vect.size(); i++)
  104. {
  105. ss << (vect[i] == null ? "null" : to_string(vect[i]));
  106. ss << (i < vect.size() - 1 ? "," : "]");
  107. }
  108. return ss.str();
  109. }
  110. int main()
  111. {
  112. vector<int> nums = {1,null,2,3};
  113. TreeNode *root = buildTree(nums);
  114. preorderPrint(root);
  115. preorderPrint2(root);
  116. cout << vectorToString(preorderTraversal(root)) << endl;
  117. cout << vectorToString(preorderTraversal2(root)) << endl;
  118. nums = {3,9,20,null,null,15,7};
  119. root = buildTree(nums);
  120. preorderPrint(root);
  121. preorderPrint2(root);
  122. cout << vectorToString(preorderTraversal(root)) << endl;
  123. cout << vectorToString(preorderTraversal2(root)) << endl;
  124. nums = {1,2,3,4,5,6,7};
  125. root = buildTree(nums);
  126. preorderPrint(root);
  127. preorderPrint2(root);
  128. cout << vectorToString(preorderTraversal(root)) << endl;
  129. cout << vectorToString(preorderTraversal2(root)) << endl;
  130. return 0;
  131. }

输出:

1 2 3
1 2 3
[1,2,3]
[1,2,3]
3 9 20 15 7
3 9 20 15 7
[3,9,20,15,7]
[3,9,20,15,7]
1 2 4 5 3 6 7
1 2 4 5 3 6 7
[1,2,4,5,3,6,7]
[1,2,4,5,3,6,7]

中序后序对比:

  1. //中序遍历
  2. void inorder(TreeNode* root) {
  3. stack<TreeNode*> st;
  4. TreeNode* node = root;
  5. while (!st.empty() || node != nullptr) {
  6. while (node != nullptr) {
  7. st.push(node);
  8. node = node->left;
  9. }
  10. node = st.top();
  11. st.pop();
  12. cout << node->val << " ";
  13. node = node->right;
  14. }
  15. }
  16. //后序遍历
  17. void postorder(TreeNode* root) {
  18. stack<TreeNode*> st;
  19. TreeNode* node = root;
  20. TreeNode* last = nullptr;
  21. while (!st.empty() || node != nullptr) {
  22. while (node != nullptr) {
  23. st.push(node);
  24. node = node->left;
  25. }
  26. node = st.top();
  27. if (node->right == nullptr || node->right == last) {
  28. cout << node->val << " ";
  29. st.pop();
  30. last = node;
  31. node = nullptr;
  32. } else {
  33. node = node->right;
  34. }
  35. }
  36. }

2. 二叉树的最大深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

     3
    / \
   9  20
 /  \
15   7

返回它的最大深度 3 。

出处:

https://bbs.csdn.net/topics/604364348

代码:

  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <queue>
  5. #define null INT_MIN
  6. using namespace std;
  7. struct TreeNode
  8. {
  9. int val;
  10. TreeNode *left, *right;
  11. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  12. };
  13. class Solution
  14. {
  15. public:
  16. int maxDepth(TreeNode* root) {
  17. if (!root) return 0;
  18. stack<pair<TreeNode*, int>> s;
  19. int maxDep = 0;
  20. s.push(make_pair(root, 1));
  21. while (!s.empty()) {
  22. TreeNode* cur = s.top().first;
  23. int curDep = s.top().second;
  24. s.pop();
  25. if (cur) {
  26. maxDep = max(maxDep, curDep);
  27. s.push(make_pair(cur->left, curDep + 1));
  28. s.push(make_pair(cur->right, curDep + 1));
  29. }
  30. }
  31. return maxDep;
  32. }
  33. };
  34. TreeNode* buildTree(vector<int>& nums)
  35. {
  36. if (nums.empty()) return nullptr;
  37. TreeNode *root = new TreeNode(nums.front());
  38. queue<TreeNode*> q;
  39. q.push(root);
  40. int i = 1;
  41. while(!q.empty() && i < nums.size())
  42. {
  43. TreeNode *cur = q.front();
  44. q.pop();
  45. if(i < nums.size() && nums[i] != null)
  46. {
  47. cur->left = new TreeNode(nums[i]);
  48. q.push(cur->left);
  49. }
  50. i++;
  51. if(i < nums.size() && nums[i] != null)
  52. {
  53. cur->right = new TreeNode(nums[i]);
  54. q.push(cur->right);
  55. }
  56. i++;
  57. }
  58. return root;
  59. }
  60. int main()
  61. {
  62. Solution s;
  63. vector<int> nums = {3,9,20,null,null,15,7};
  64. TreeNode *root = buildTree(nums);
  65. cout << s.maxDepth(root) << endl;
  66. nums = {1,null,2,null,3,null,4};
  67. root = buildTree(nums);
  68. cout << s.maxDepth(root) << endl;
  69. return 0;
  70. }

输出:

3
4

递归法:

  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <queue>
  5. #define null INT_MIN
  6. using namespace std;
  7. struct TreeNode
  8. {
  9. int val;
  10. TreeNode *left, *right;
  11. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  12. };
  13. class Solution
  14. {
  15. public:
  16. int maxDepth(TreeNode* root) {
  17. if (!root) return 0;
  18. int ldepth = maxDepth(root->left);
  19. int rdepth = maxDepth(root->right);
  20. return max(ldepth, rdepth) + 1;
  21. }
  22. };
  23. TreeNode* buildTree(vector<int>& nums)
  24. {
  25. if (nums.empty()) return nullptr;
  26. TreeNode *root = new TreeNode(nums.front());
  27. queue<TreeNode*> q;
  28. q.push(root);
  29. int i = 1;
  30. while(!q.empty() && i < nums.size())
  31. {
  32. TreeNode *cur = q.front();
  33. q.pop();
  34. if(i < nums.size() && nums[i] != null)
  35. {
  36. cur->left = new TreeNode(nums[i]);
  37. q.push(cur->left);
  38. }
  39. i++;
  40. if(i < nums.size() && nums[i] != null)
  41. {
  42. cur->right = new TreeNode(nums[i]);
  43. q.push(cur->right);
  44. }
  45. i++;
  46. }
  47. return root;
  48. }
  49. int main()
  50. {
  51. Solution s;
  52. vector<int> nums = {3,9,20,null,null,15,7};
  53. TreeNode *root = buildTree(nums);
  54. cout << s.maxDepth(root) << endl;
  55. nums = {1,null,2,null,3,null,4};
  56. root = buildTree(nums);
  57. cout << s.maxDepth(root) << endl;
  58. return 0;
  59. }

3. 有序数组转换为二叉搜索树

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。

高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

示例 1:

输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:

示例 2:

输入:nums = [1,3]
输出:[3,1]
解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。

提示:

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums 按 严格递增 顺序排列

出处:

https://edu.csdn.net/practice/23819519

代码:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <queue>
  5. #define null INT_MIN
  6. using namespace std;
  7. struct TreeNode
  8. {
  9. int val;
  10. TreeNode *left, *right;
  11. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  12. };
  13. class Solution
  14. {
  15. public:
  16. TreeNode *sortedArrayToBST(vector<int> &nums)
  17. {
  18. return dfs(nums, 0, nums.size() - 1);
  19. }
  20. TreeNode *dfs(vector<int> &nums, int left, int right)
  21. {
  22. if (left > right)
  23. return NULL;
  24. int mid = (left + right) / 2;
  25. TreeNode *t = new TreeNode(nums[mid]);
  26. t->left = dfs(nums, left, mid - 1);
  27. t->right = dfs(nums, mid + 1, right);
  28. return t;
  29. }
  30. };
  31. vector<int> levelOrder(TreeNode* root) {
  32. vector<int> res;
  33. if (!root) return res;
  34. queue<TreeNode*> q;
  35. q.push(root);
  36. while (!q.empty()) {
  37. TreeNode* cur = q.front();
  38. q.pop();
  39. if (cur) {
  40. res.push_back(cur->val);
  41. q.push(cur->left);
  42. q.push(cur->right);
  43. } else {
  44. res.push_back(null);
  45. }
  46. }
  47. for(int i = res.size(); i > 0 && res[i-1] == null; i--)
  48. res.pop_back();
  49. return res;
  50. }
  51. string vectorToString(vector<int> vect) {
  52. stringstream ss;
  53. ss << "[";
  54. for (size_t i = 0; i < vect.size(); i++)
  55. {
  56. ss << (vect[i] == null ? "null" : to_string(vect[i]));
  57. ss << (i < vect.size() - 1 ? "," : "]");
  58. }
  59. return ss.str();
  60. }
  61. int main()
  62. {
  63. Solution s;
  64. vector<int> nums = {-10,-3,0,5,9};
  65. TreeNode *bst = s.sortedArrayToBST(nums);
  66. cout << vectorToString(levelOrder(bst)) << endl;
  67. nums = {1,3};
  68. bst = s.sortedArrayToBST(nums);
  69. cout << vectorToString(levelOrder(bst)) << endl;
  70. return 0;
  71. }

输出:

[0,-10,5,null,-3,null,9]
[1,null,3]

代码2:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <queue>
  5. #define null INT_MIN
  6. using namespace std;
  7. struct TreeNode
  8. {
  9. int val;
  10. TreeNode *left, *right;
  11. TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  12. };
  13. class Solution
  14. {
  15. public:
  16. TreeNode* sortedArrayToBST(vector<int>& nums) {
  17. if (nums.empty()) return nullptr;
  18. int mid = nums.size() / 2;
  19. TreeNode* root = new TreeNode(nums[mid]);
  20. vector<int> left(nums.begin(), nums.begin() + mid);
  21. vector<int> right(nums.begin() + mid + 1, nums.end());
  22. root->left = sortedArrayToBST(left);
  23. root->right = sortedArrayToBST(right);
  24. return root;
  25. }
  26. };
  27. vector<int> levelOrder(TreeNode* root) {
  28. vector<int> res;
  29. if (!root) return res;
  30. queue<TreeNode*> q;
  31. q.push(root);
  32. while (!q.empty()) {
  33. TreeNode* cur = q.front();
  34. q.pop();
  35. if (cur) {
  36. res.push_back(cur->val);
  37. q.push(cur->left);
  38. q.push(cur->right);
  39. } else {
  40. res.push_back(null);
  41. }
  42. }
  43. for(int i = res.size(); i > 0 && res[i-1] == null; i--)
  44. res.pop_back();
  45. return res;
  46. }
  47. string vectorToString(vector<int> vect) {
  48. stringstream ss;
  49. ss << "[";
  50. for (size_t i = 0; i < vect.size(); i++)
  51. {
  52. ss << (vect[i] == null ? "null" : to_string(vect[i]));
  53. ss << (i < vect.size() - 1 ? "," : "]");
  54. }
  55. return ss.str();
  56. }
  57. int main()
  58. {
  59. Solution s;
  60. vector<int> nums = {-10,-3,0,5,9};
  61. TreeNode *bst = s.sortedArrayToBST(nums);
  62. cout << vectorToString(levelOrder(bst)) << endl;
  63. nums = {1,3};
  64. bst = s.sortedArrayToBST(nums);
  65. cout << vectorToString(levelOrder(bst)) << endl;
  66. return 0;
  67. }

输出:

[0,-3,9,-10,null,5]
[3,1]


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览43883 人正在系统学习中
PythonTogether
微信公众号
一起来学派森吧