- 二叉树的前序遍历(144. Binary Tree Preorder Traversal)
递归
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void perorderTraversalHelper(vector<int> &vec, TreeNode *root) 13 { 14 if (root == NULL) 15 { 16 return; 17 } 18 19 vec.push_back(root->val); 20 perorderTraversalHelper(vec, root->left); 21 perorderTraversalHelper(vec, root->right); 22 } 23 24 vector<int> preorderTraversal(TreeNode* root) { 25 vector<int> vec; 26 perorderTraversalHelper(vec, root); 27 return vec; 28 } 29 };
非递归
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> preorderTraversal(TreeNode* root) { 13 vector<int> vec; 14 15 TreeNode *p = root; 16 stack<TreeNode *> nodeStack; 17 18 while (p != NULL || !nodeStack.empty()) 19 { 20 while (p != NULL) 21 { 22 vec.push_back(p->val); 23 nodeStack.push(p); 24 p = p->left; 25 } 26 27 if (!nodeStack.empty()) 28 { 29 TreeNode *node = nodeStack.top(); 30 nodeStack.pop(); 31 p = node->right; 32 } 33 } 34 35 return vec; 36 } 37 };
- 二叉树的中序遍历(94. Binary Tree Inorder Traversal)
递归
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void inorderTraversalHelper(vector<int> &vec, TreeNode *root) 13 { 14 if (root == NULL) 15 { 16 return; 17 } 18 19 inorderTraversalHelper(vec, root->left); 20 vec.push_back(root->val); 21 inorderTraversalHelper(vec, root->right); 22 } 23 24 vector<int> inorderTraversal(TreeNode* root) { 25 vector<int> vec; 26 inorderTraversalHelper(vec, root); 27 return vec; 28 } 29 };
非递归
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> inorderTraversal(TreeNode* root) { 13 14 vector<int> vec; 15 16 TreeNode *p = root; 17 stack<TreeNode *> nodeStack; 18 19 while (p != NULL || !nodeStack.empty()) 20 { 21 while (p != NULL) 22 { 23 nodeStack.push(p); 24 p = p->left; 25 } 26 27 if (!nodeStack.empty()) 28 { 29 TreeNode *node = nodeStack.top(); 30 nodeStack.pop(); 31 vec.push_back(node->val); 32 33 p = node->right; 34 } 35 } 36 37 return vec; 38 } 39 };
- 二叉树的后序遍历
递归
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 void postorderTraversalHelper(vector<int> &vec, TreeNode *root) 13 { 14 if (root == NULL) 15 { 16 return; 17 } 18 19 postorderTraversalHelper(vec, root->left); 20 postorderTraversalHelper(vec, root->right); 21 vec.push_back(root->val); 22 } 23 24 vector<int> postorderTraversal(TreeNode* root) { 25 vector<int> vec; 26 postorderTraversalHelper(vec, root); 27 return vec; 28 } 29 };
时间: 2024-10-25 18:39:26