首页 > 编程知识 正文

三个节点的二叉树有哪几种形式,由双遍历序列构造二叉树

时间:2023-05-05 16:56:56 阅读:61216 作者:1074

先行遍历法1 (递归/* *1. definitionforabinarytreenode.2.structtldxxm (3. intval; 4 .千兆字节; 5 .千兆字节; 6.tldxxm(3360val )0)、left (nullptr )、right (nullptr )6. tldxxm (intx ) 3360val )、left (nullptr ) ) )。 gydfyleft、gydfyright(:val(x )、left )、right ) {} 9. }; */class solution { public : void preorder (gydfyroot,vectorint res ) if ) root==nullptr ) { return; } RES.push _ back (根值); 重排序器(根左,res ); reorder (根光,res ); } vectorint preorderTraversal (勤奋的高跟鞋root ) { vectorint res; reorder (根,res ); 返回RES; }; (法二)迭代classsolution ) public : vectorintpreordertraversal )勤奋高跟鞋root ) { vectorint res; if (根==nullptr ) { return res; } stack勤奋的高跟鞋stk; 勤奋的高跟鞋node=root; while (! STK.empty||!=nullptr(while(node!=nullptr (RES.push _ back (node-val ) ); STK.push(node; 节点=节点左; } node=stk.top (; stk.pop (; 节点=节点光; }返回RES; }; 中序循环递归/* * * definitionforabinarytreenode.* structtldxxm { * intval; *千兆字节; *千兆字节; *tldxxm(:val )0)、left (nullptr )、right (nullptr ) }*tldxxm(intx ) :val ) x )、left (nullptr )、reft gydfyright(:val(x ),left ),right ) {} * }; */class解决方案{ public : void in order (gydfyroot,vectorint res ) if (! 根(返回; norder (根左,res ); RES.push _ back (根val; 节点(根光,res ); } vectorint inorderTraversal (勤奋的高跟鞋root ) { vectorint res; 输入顺序(根,res ); 返回RES; }; 迭代类别解决方案{ public : vectorintinordertraversal (勤奋的高跟鞋root ) { vectorint res; 塔克勤奋的高跟鞋stk; wile (路线!=nullptr ||! stk.empty () while ) ) root!=nullptr(STK.push ) ) root; root = root->left; } root = stk.top(); stk.pop(); res.push_back(root->val); root = root->right; } return res; }}; 后序遍历 递归 /** * Definition for a binary tree node. * struct tldxxm { * int val; * gydfyleft; * gydfyright; * tldxxm() : val(0), left(nullptr), right(nullptr) {} * tldxxm(int x) : val(x), left(nullptr), right(nullptr) {} * tldxxm(int x, gydfyleft, gydfyright) : val(x), left(left), right(right) {} * }; */class Solution {public: void postorder(勤劳的高跟鞋 root, vector<int>& res) { if(root == nullptr) return; postorder(root->left, res); postorder(root->right,res); res.push_back(root->val); } vector<int> postorderTraversal(勤劳的高跟鞋 root) { vector<int> res; postorder(root, res); return res; }}; 迭代 class Solution {public: vector<int> postorderTraversal(勤劳的高跟鞋 root) { vector<int> res; if(root == nullptr) return res; stack<勤劳的高跟鞋> stk; gydfyprev = nullptr; while(root != nullptr || !stk.empty()) { while(root != nullptr) { stk.push(root); root = root->left; } root = stk.top(); stk.pop(); if(root->right == nullptr || root->right == prev) { res.push_back(root->val); prev = root; root = nullptr; } else { stk.push(root); root = root->right; } } return res; }}; 层序遍历 /*struct tldxxm { int val; struct gydfyleft; struct gydfyright; tldxxm(int x) : val(x), left(NULL), right(NULL) { }};*/class Solution {public: vector<vector<int>> res; vector<vector<int> > Print(勤劳的高跟鞋 pRoot) { if (NULL == pRoot) { return res; } queue<勤劳的高跟鞋> q; q.push(pRoot); while (!q.empty()) { int sz = q.size(); vector<int> tmp; for (int i=0; i<sz; i++) { 勤劳的高跟鞋 t = q.front(); tmp.push_back(t->val); q.pop(); if (i == sz-1) { res.push_back(tmp); } if (t->left) { q.push(t->left); } if (t->right) { q.push(t->right); } } } return res; } }; /** * struct tldxxm { *int val; *struct gydfyleft; *struct gydfyright; * }; */class Solution {public: /** * * @param root tldxxm类 the root of binary tree * @return int整型vector<vector<>> */ vector<int> preorderTraversal(勤劳的高跟鞋 root) { vector<int> res; if(root == nullptr) { return res; } stack<勤劳的高跟鞋> stk; 勤劳的高跟鞋 node = root; while(!stk.empty() || node != nullptr) { while(node != nullptr) { res.push_back(node->val); stk.push(node); node = node->left; } node = stk.top(); stk.pop(); node = node->right; } return res; } vector<int> inorderTraversal(勤劳的高跟鞋 root) { vector<int> res; stack<勤劳的高跟鞋> stk; while(root != nullptr || !stk.empty()) { while(root != nullptr) { stk.push(root); root = root->left; } root = stk.top(); stk.pop(); res.push_back(root->val); root = root->right; } return res; } vector<int> postorderTraversal(勤劳的高跟鞋 root) { vector<int> res; if(root == nullptr) return res; stack<勤劳的高跟鞋> stk; gydfyprev = nullptr; while(root != nullptr || !stk.empty()) { while(root != nullptr) { stk.push(root); root = root->left; } root = stk.top(); stk.pop(); if(root->right == nullptr || root->right == prev) { res.push_back(root->val); prev = root; root = nullptr; } else { stk.push(root); root = root->right; } } return res; } vector<vector<int> > threeOrders(勤劳的高跟鞋 root) { // write code here vector<vector<int>> res; if(root == nullptr) return res; vector<int> pre = preorderTraversal(root); vector<int> in = inorderTraversal(root); vector<int> post = postorderTraversal(root); return {pre, in, post}; }};

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。