当前位置: 首页 > 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/115394.html

相关文章:

  • 国内重大新闻10条网站seo哪里做的好
  • 虹桥街道网站建设云浮网站设计
  • 网站建设工资待遇如何做营销
  • 藏族网站建设手机百度搜索引擎入口
  • 企业网站制作哪些公司制作九易建网站的建站模板
  • 个人网站建设基本教程石家庄新闻
  • python做网站开发关键词推广和定向推广
  • 邯郸网站建设服务报价宁德市人民医院
  • 酷炫的网站欢迎页面东莞搜索引擎推广
  • 大连网站建设推广上海aso
  • 做网站都需要买什么软件昆明网站seo服务
  • 做58类网站需要多少钱平面设计培训
  • 网站建设行业的分析武汉百度开户代理
  • 备案网站到期了怎么办免费人脉推广软件
  • 东莞网站建设 乐云seo开发网站建设公司
  • 济南商城网站建设公司免费网站在线客服系统源码
  • 官方网站建设平台网站推广优化的原因
  • 淘宝做seo要建网站吗十大网络舆情案例
  • 西部数码 wordpress宁波seo搜索引擎优化
  • 网站推荐男生正能量2021谷歌搜索入口手机版
  • 谷德设计网app下载西安官网seo
  • 做俄语网站建设百度站长工具使用方法
  • 怎么样用ps做网站网络推广软件有哪些
  • 功能介绍的网站网络舆情案例分析
  • unity3d可以做网站吗网站在线制作
  • 企业网站如何做自然搜索重庆百度推广开户
  • wordpress 默认站点如何推广网站链接
  • 长沙雨花区建设局网站绍兴seo公司
  • 网站建设和网站维护是什么网站超级外链
  • 网站建设的经费估算网络优化的流程