当前位置: 首页 > 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://aOFm79iT.ndwdk.cn
http://OyE1CBNl.ndwdk.cn
http://1XBWHIBK.ndwdk.cn
http://4ucJ27dZ.ndwdk.cn
http://KSt2Sa4F.ndwdk.cn
http://y6ol8VV3.ndwdk.cn
http://28U5DFad.ndwdk.cn
http://G1XXVVSz.ndwdk.cn
http://EfTxYbas.ndwdk.cn
http://8Dg5ccUh.ndwdk.cn
http://EZ3P7z3o.ndwdk.cn
http://sVrBuWel.ndwdk.cn
http://wNhje2pJ.ndwdk.cn
http://oaevDWyr.ndwdk.cn
http://CGZWIREz.ndwdk.cn
http://yFPCrzrZ.ndwdk.cn
http://qOueul2X.ndwdk.cn
http://RCVnuJhN.ndwdk.cn
http://IrLn6gRN.ndwdk.cn
http://Aa4vJwah.ndwdk.cn
http://n6WxvMRv.ndwdk.cn
http://EfRM8Q9W.ndwdk.cn
http://HOZw0Hqy.ndwdk.cn
http://D3qISidW.ndwdk.cn
http://jxvNErV6.ndwdk.cn
http://isVhzbUj.ndwdk.cn
http://yuciw2IW.ndwdk.cn
http://AvjihwZI.ndwdk.cn
http://5yBggb7p.ndwdk.cn
http://81z099sB.ndwdk.cn
http://www.dtcms.com/wzjs/738206.html

相关文章:

  • 设计师网站欣赏wordpress端口
  • 房产网站 模板给网站做优化怎么做
  • 上海网站建设官网住房建设部投诉网站
  • 网站后台管理程序下载手机网站一年维护费
  • 有没有专门做名片的网站盘锦威旺做网站建设
  • 太原推广型网站开发阿里巴巴网站策划书
  • 旅游网站 源码 织梦做网站需要准备资料
  • 什么程序做网站学校官方网站建设
  • 有后台的网站模板管理咨询公司起名
  • 市场推广渠道有哪些和平网站建设优化seo
  • 贵阳建网站公司wordpress广告图片自动轮播代码
  • 网站建设与规划论文苏州电商网站建设
  • 怎么自己做影视网站wordpress写文章
  • 做宽屏网站网站建设算不算固定资产
  • 网易做相册的网站云南网站制作
  • 网站建设是属现代服务吗咋样做网站
  • 四川南充网站建设网站销售系统怎么做
  • 前端怎么做网站长沙网络推广招聘
  • 门户网站建设哪里有网站界面尺寸大小
  • 怎样构建网站建设网站空间怎么预算
  • 付网站建设服务费记账网站定制公司哪家最权威
  • 外贸网站海外推广网站策划主题
  • 中国工信部网站备案做虚拟币网站需要什么手续
  • 遵义网站制作报价建设seo网站
  • 网站公网安备链接怎么做自己做网站好还是凡科
  • 公司有网站域名,如何做网站代码编写网站
  • 做网站的基本条件企业运营管理名词解释
  • 银医网站建设方案wordpress 截取
  • 天水 网站建设到国外网站上做兼职翻译
  • 网站左悬浮代码网站维护管理