这里就用力扣上面的各种题目要模板了。
102. 二叉树的层序遍历
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> ans; if(!root) return ans; queue<TreeNode*> q; q.push(root); while(!q.empty()){ int size = q.size(); vector<int> list; while(size--){ TreeNode* r = q.front(); q.pop(); list.push_back(r->val); if(r->left) q.push(r->left); if(r->right) q.push(r->right); } if(list.size()){ ans.push_back(list); } } return ans; } };144. 二叉树的前序遍历 94. 二叉树的中序遍历 145. 二叉树的后序遍历
// 先序 class Solution { public: vector<int> ans; vector<int> preorderTraversal(TreeNode* root) { preOrder(root); return ans; } void preOrder(TreeNode* root){ if(!root) return; ans.push_back(root->val); preOrder(root->left); preOrder(root->right); } }; // 中序 ```cpp class Solution { public: vector<int> ans; vector<int> inorderTraversal(TreeNode* root) { inOrder(root); return ans; } void inOrder(TreeNode* root){ if(!root) return; inOrder(root->left); ans.push_back(root->val); inOrder(root->right); } }; // 后序 class Solution { public: vector<int> ans; vector<int> postorderTraversal(TreeNode* root) { postorder(root); return ans; } void postorder(TreeNode* root){ if(!root) return; postorder(root->left); postorder(root->right); ans.push_back(root->val); } };挖个坑,以后填吧
105. 从前序与中序遍历序列构造二叉树
class Solution { public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { int n = preorder.size(); return create(0,n-1,0,n-1,preorder,inorder); } TreeNode* create(int preL,int preR,int inL,int inR,vector<int>& preorder, vector<int>& inorder){ if(preL>preR){ return nullptr; } int val = preorder[preL]; TreeNode* node = new TreeNode(val); int idx = 0; for(int i=inL;i<=inR;i++){ if(inorder[i]==val){ idx = i; break; } } int numLeft = idx-inL; node->left = create(preL+1,preL+numLeft,inL,idx-1,preorder,inorder); node->right = create(preL+numLeft+1,preR,idx+1,inR,preorder,inorder); return node; } };106. 从中序与后序遍历序列构造二叉树
class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { int n = inorder.size(); return create(0,n-1,0,n-1,inorder,postorder); } TreeNode* create(int postL,int postR,int inL,int inR,vector<int>& inorder, vector<int>& postorder){ if(postL>postR){ return nullptr; } TreeNode* root = new TreeNode(postorder[postR]); int idx = 0; for(int i = inL;i<=inR;i++){ if(inorder[i]==postorder[postR]){ idx = i; break; } } int numLeft = idx-inL; root->left = create(postL,postL+numLeft-1,inL,idx-1,inorder,postorder); root->right = create(postL+numLeft,postR-1,idx+1,inR,inorder,postorder); return root; } };先序遍历与后序遍历序列,求其中序遍历序列
题意: 给定一棵有n个结点的二叉树的先序遍历与后序遍历序列,求其中序遍历序列。 若某节点只有一个子结点,则此处将其看作左儿子结点 (题目来自牛客网)做法也是一样的,找出根节点,然后根据规则,划分左右子树。
class Solution { private: struct Node{ int val; Node* left= nullptr,*right= nullptr; Node(int val):val(val){} }; Node* root; public: /** * 返回所求中序序列 * @param n int整型 二叉树节点数量 * @param pre int整型vector 前序序列 * @param suf int整型vector 后序序列 * @return int整型vector */ void inOrder(Node* p){ if(p== nullptr) return; inOrder(p->left); inOrderList.push_back(p->val); inOrder(p->right); } Node* create(vector<int>& pre,int pl,int pr, vector<int>& suf,int sl,int sr){ if(pl>pr) return nullptr; Node* p = new Node(pre[pl]); if(pl<pr){ int idx = -1; for(int i=sl;i<=sr;i++){ if(suf[i] == pre[pl+1]){ idx = i; break; } } int lenOfLeft = idx-sl+1; p->left = create(pre,pl+1,pl+lenOfLeft,suf,sl,sl+lenOfLeft-1); p->right = create(pre,pl+lenOfLeft+1,pr,suf,sl+lenOfLeft,sr-1); } return p; } vector<int> inOrderList; vector<int> solve(int n, vector<int>& pre, vector<int>& suf) { root = create(pre,0,n-1,suf,0,n-1); inOrder(root); return inOrderList; } };暂时没找到相关题目。 大致思路是,根据层次遍历的有序性
别人的博客