当前位置: 首页 > wzjs >正文

优秀网站免费网站建设哪个好

优秀网站,免费网站建设哪个好,做网站不好做,医院做网站需要备案吗利用递归来遍历树,实际的递归中隐式利用了栈,在此我们可以直接模拟递归中栈的调用。在后序遍历中从左向右依次先序遍历该每个以子节点为根的子树,然后先遍历节点本身。在这里的栈模拟中比较难处理的在于从当前节点 u 的子节点 v1 返回时&…

利用递归来遍历树,实际的递归中隐式利用了栈,在此我们可以直接模拟递归中栈的调用。在后序遍历中从左向右依次先序遍历该每个以子节点为根的子树,然后先遍历节点本身。在这里的栈模拟中比较难处理的在于从当前节点 u 的子节点 v1 返回时,此时需要处理节点 u 的下一个节点 v2 ,此时需要记录当前已经遍历完成哪些子节点,才能找到下一个需要遍历的节点。在二叉树树中因为只有左右两个子节点,因此比较方便处理,在 N 叉树中由于有多个子节点,因此使用哈希表记录当前节点 u 已经访问过哪些子节点。

每次入栈时都将当前。节点的 u 的第一个子节点压入栈中,直到当前节点为空节点为止;

每次查看栈顶元素 p ,如果节点 p 的子节点已经全部访问过,则记录当前节点的值,并将节点 p 的从栈中弹出,并从哈希表中移除,表示该以该节点的子树已经全部遍历过;如果当前节点 p 的子节点还有未遍历的,则将当前节点的 p 的下一个未访问的节点压入到栈中,重复上述的入栈操作。

代码

Java

class Solution {public List<Integer> postorder(Node root) {List<Integer> res = new ArrayList<Integer>();if (root == null) {return res;}Map<Node, Integer> map = new HashMap<Node, Integer>();Deque<Node> stack = new ArrayDeque<Node>();Node node = root;while (!stack.isEmpty() || node != null) {while (node != null) {stack.push(node);List<Node> children = node.children;if (children != null && children.size() > 0) {map.put(node, 0);node = children.get(0);} else {node = null;}}node = stack.peek();int index = map.getOrDefault(node, -1) + 1;List<Node> children = node.children;if (children != null && children.size() > index) {map.put(node, index);node = children.get(index);} else {res.add(node.val);stack.pop();map.remove(node);node = null;}}return res;}
}

C++

class Solution {
public:vector<int> postorder(Node* root) {vector<int> res;if (root == nullptr) {return res;}unordered_map<Node *, int> cnt;stack<Node *> st;Node * node = root;while (!st.empty() || node != nullptr) {while (node != nullptr) {st.emplace(node);if (node->children.size() > 0) {cnt[node] = 0;node = node->children[0];} else {node = nullptr;}         }node = st.top();int index = cnt.count(node) ? (cnt[node] + 1) : 0;if (index < node->children.size()) {cnt[node] = index;node = node->children[index];} else {res.emplace_back(node->val);st.pop();cnt.erase(node);node = nullptr;}}return res;}
};

C#

public class Solution {public IList<int> Postorder(Node root) {IList<int> res = new List<int>();if (root == null) {return res;}Dictionary<Node, int> dictionary = new Dictionary<Node, int>();Stack<Node> stack = new Stack<Node>();Node node = root;while (stack.Count > 0 || node != null) {while (node != null) {stack.Push(node);IList<Node> childrenList = node.children;if (childrenList != null && childrenList.Count > 0) {dictionary.Add(node, 0);node = childrenList[0];} else {node = null;}}node = stack.Peek();int index = (dictionary.ContainsKey(node) ? dictionary[node] : -1) + 1;IList<Node> children = node.children;if (children != null && children.Count > index) {dictionary[node] = index;node = children[index];} else {res.Add(node.val);stack.Pop();dictionary.Remove(node);node = null;}}return res;}
}     

好了,今天的文章分享就到这里了,希望对大家的学习有帮助哦! 

http://www.dtcms.com/wzjs/259876.html

相关文章:

  • 南京手机网站设计个人博客网站设计毕业论文
  • 做动态图片的网站吗安卓优化大师旧版本下载
  • 中文网站建设中模板在线的crm系统软件
  • 阿里云网站空间购买网店关键词怎么优化
  • 为网站吸引流量的方法郑州网络营销排名
  • 手机网站大小2345纯净版推广包
  • 网站建设零基础自学视频教程设计外包网站
  • 佛山网站建设优势网站免费发布与推广
  • 公司做网站费用计什么科目网上推广赚钱方法
  • 昆明网站建设天猫运营2022十大网络营销案例
  • 网站如何做超级链接手机百度下载免费安装
  • 怎么在网站上打广告谷歌seo排名优化
  • 国家企业官网查询系统搜索引擎优化方法有哪些
  • 毕设做网站具体步骤排名函数
  • 广东营销网站建设竞价推广价格
  • 网站如何做双链路关键词小说
  • 响应式外贸网站建设网站的优化与推广分析
  • 做短视频的网站收益qq群推广引流免费网站
  • seo岗位是什么意思seo赚钱方法大揭秘
  • dreamweaver做动态网站网络销售怎么干
  • 京东网站建设现状分析搜索推广出价多少合适
  • 公司网站忘了怎么做微博指数查询入口
  • 做装修网站郑州seo优化
  • wordpress 帮助插件seo快速排名
  • 网站建设的技术有哪些方面成都seo培
  • discuz怎么做网站网上国网app推广方案
  • 重庆网领网站建设公司网站备案流程
  • 凡客建站网长春百度seo公司
  • 昆明网站制作网页网络营销所学课程
  • 广州站在哪个区教师遭网课入侵直播录屏曝光广场舞