## 二叉树节点的定义模板
C++
1 2 3 4 5 6
| struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x): val(x), left(NULL), right(NULL) {} }
|
Java
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } }
|
二叉树的递归遍历模板
关于递归构建思路的补充:确定递归的三要素
确定递归函数的参数和返回值
1
| void traveral(TreeNode* cur, vector<int>& vec)
|
确定终止条件
1
| if (cur == NULL) return;
|
- 确定单层递归的逻辑(以前序遍历为例)
1 2 3
| vec.push_back(cur->val); traversal(cur->left, vec); traversal(cur->right, vec);
|
前序遍历(递归)
https://leetcode.cn/problems/binary-tree-preorder-traversal/
1 2 3 4 5 6 7 8 9 10 11
| void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL) return; vec.push_back(cur->val); traversal(cur->left, vec); traversal(cur->right, vec); } vector<int> preorderTraversal(TreeNode* root) { vector<int> result; traversal(root, result); return result; }
|
中序遍历(递归)
https://leetcode.cn/problems/binary-tree-inorder-traversal/
1 2 3 4 5 6 7 8 9 10 11
| void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL) return; traversal(cur->left, vec); vec.push_back(cur->val); traversal(cur->right, vec); } vector<int> preorderTraversal(TreeNode* root) { vector<int> result; traversal(root, result); return result; }
|
后序遍历(递归)
https://leetcode.cn/problems/binary-tree-postorder-traversal/
1 2 3 4 5 6 7 8 9 10 11
| void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL) return; traversal(cur->left, vec); traversal(cur->right, vec); vec.push_back(cur->val); } vector<int> preorderTraversal(TreeNode* root) { vector<int> result; traversal(root, result); return result; }
|
Java版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<Integer>(); preorder(root, result); return result; } public void preorder(TreeNode root, List<Integer> list) { if (root == null) return; list.add(root.val); preorder(root.left, list); preorder(root.right, list); } }
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); inorder(root, res); return res; } void inorder(TreeNode root, List<Integer> list) { if (root == null) return; inorder(root.left, list); list.add(root.val); inorder(root.right, list); } }
class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); postorder(root, res); return res; } void postorder(TreeNode root, List<Integer> list) { if (root == null) return; postorder(root.left, list); postorder(root.right, list); list.add(root.val); } }
|
二叉树的迭代遍历
前序遍历(迭代)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| vector<int> preorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; if (root == NULL) return result; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); result.push_back(node->val); if (node->right) st.push(node->right); if (node->left) st.push(node->left); } return result; }
|
中序遍历(迭代)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; TreeNode* cur = root; while (cur != NULL || !st.empty()) { if (cur != NULL) { st.push(cur); cur = cur->left; } else { cur = st.top(); st.pop(); result.push_back(cur->val); cur = cur->right; } } return result; }
|
后序遍历(迭代)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public: vector<int> postorderTraversal(TreeNode* root) { vector<int> result; stack<TreeNode*> st; if (root == NULL) return result; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); result.push_back(cur->val); if (node->left) st.push(node->left); if (node->right) st.push(node->right); } reverse(result.begin(), result.end()); return result; }
|
“此时我们用迭代法写出了二叉树的前后中序遍历,大家可以看出前序和中序是完全两种代码风格,并不像递归写法那样代码稍做调整,就可以实现前后中序。
这是因为前序遍历中访问节点(遍历节点)和处理节点(将元素放进result数组中)可以同步处理,但是中序就无法做到同步!“