LintCode: Binary Tree Inorder Traversal

╰半夏微凉° 2021-09-19 09:40 344阅读 0赞

C++,递归,辅助函数

复制代码

  1. 1 /**
  2. 2 * Definition of TreeNode:
  3. 3 * class TreeNode {
  4. 4 * public:
  5. 5 * int val;
  6. 6 * TreeNode *left, *right;
  7. 7 * TreeNode(int val) {
  8. 8 * this->val = val;
  9. 9 * this->left = this->right = NULL;
  10. 10 * }
  11. 11 * }
  12. 12 */
  13. 13 class Solution {
  14. 14 /**
  15. 15 * @param root: The root of binary tree.
  16. 16 * @return: Inorder in vector which contains node values.
  17. 17 */
  18. 18 public:
  19. 19 vector<int> inorderTraversal(TreeNode *root) {
  20. 20 // write your code here
  21. 21 vector<int> result;
  22. 22 if (root == NULL) {
  23. 23 return result;
  24. 24 } else {
  25. 25 inorderCore(root, result);
  26. 26 }
  27. 27 return result;
  28. 28 }
  29. 29 void inorderCore(TreeNode *root, vector<int> &result) {
  30. 30 if (root->left != NULL) {
  31. 31 inorderCore(root->left, result);
  32. 32 }
  33. 33 result.push_back(root->val);
  34. 34 if (root->right != NULL) {
  35. 35 inorderCore(root->right, result);
  36. 36 }
  37. 37 }
  38. 38 };

复制代码

C++,递归

复制代码

  1. 1 /**
  2. 2 * Definition of TreeNode:
  3. 3 * class TreeNode {
  4. 4 * public:
  5. 5 * int val;
  6. 6 * TreeNode *left, *right;
  7. 7 * TreeNode(int val) {
  8. 8 * this->val = val;
  9. 9 * this->left = this->right = NULL;
  10. 10 * }
  11. 11 * }
  12. 12 */
  13. 13 class Solution {
  14. 14 /**
  15. 15 * @param root: The root of binary tree.
  16. 16 * @return: Inorder in vector which contains node values.
  17. 17 */
  18. 18 public:
  19. 19 vector<int> inorderTraversal(TreeNode *root) {
  20. 20 // write your code here
  21. 21 vector<int> result;
  22. 22 if (root == NULL) {
  23. 23 return result;
  24. 24 }
  25. 25 if (root->left != NULL) {
  26. 26 vector<int> left = inorderTraversal(root->left);
  27. 27 result.reserve(result.size() + left.size());
  28. 28 result.insert(result.end(), left.begin(), left.end());
  29. 29 }
  30. 30 result.push_back(root->val);
  31. 31 if (root->right != NULL) {
  32. 32 vector<int> right = inorderTraversal(root->right);
  33. 33 result.reserve(result.size() + right.size());
  34. 34 result.insert(result.end(), right.begin(), right.end());
  35. 35 }
  36. 36 return result;
  37. 37 }
  38. 38 };

复制代码

C++,非递归

复制代码

  1. 1 /**
  2. 2 * Definition of TreeNode:
  3. 3 * class TreeNode {
  4. 4 * public:
  5. 5 * int val;
  6. 6 * TreeNode *left, *right;
  7. 7 * TreeNode(int val) {
  8. 8 * this->val = val;
  9. 9 * this->left = this->right = NULL;
  10. 10 * }
  11. 11 * }
  12. 12 */
  13. 13 class Solution {
  14. 14 /**
  15. 15 * @param root: The root of binary tree.
  16. 16 * @return: Inorder in vector which contains node values.
  17. 17 */
  18. 18 public:
  19. 19 vector<int> inorderTraversal(TreeNode *root) {
  20. 20 // write your code here
  21. 21 vector<int> result;
  22. 22 if (root == NULL) {
  23. 23 return result;
  24. 24 }
  25. 25 stack<TreeNode *> sta;
  26. 26 TreeNode *cur = root;
  27. 27 while (cur != NULL || !sta.empty()) {
  28. 28 while(cur != NULL) {
  29. 29 sta.push(cur);
  30. 30 cur = cur->left;
  31. 31 }
  32. 32 cur = sta.top();
  33. 33 sta.pop();
  34. 34 result.insert(result.end(), cur->val);
  35. 35 cur = cur->right;
  36. 36 }
  37. 37 return result;
  38. 38 }
  39. 39 };

复制代码

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4999036.html,如需转载请自行联系原作者

发表评论

表情:
评论列表 (有 0 条评论,344人围观)

还没有评论,来说两句吧...

相关阅读