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

品牌网站建设怎样在百度上建立网站

品牌网站建设,怎样在百度上建立网站,各大网站的名字大全,导出 wordpress利用递归来遍历树,实际的递归中隐式利用了栈,在此我们可以直接模拟递归中栈的调用。在后序遍历中从左向右依次先序遍历该每个以子节点为根的子树,然后先遍历节点本身。在这里的栈模拟中比较难处理的在于从当前节点 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/387516.html

相关文章:

  • 涂料网站源码网络营销案例范文
  • python 做网站缺点培训心得体会怎么写
  • 表单网站怎么做seo5g影讯5g天线在线观看免费视频
  • 关于文化的网站模板广告宣传网站
  • 做网站开发没有人带产品宣传推广方案
  • 国外公司网站设计可以进入任何网站的浏览器
  • 图片交易网站如何建设湖北seo推广
  • 微信网站建设报价单搜索引擎优化自然排名
  • 做网页要去哪个网站2021年最为成功的营销案例
  • 贵阳学网站建设全国唯一一个没有疫情的城市
  • 制作网站学什么软件表白网站制作
  • 做户外旅游网站搜狗搜索引擎入口
  • 网站魔板大全外贸高端网站设计公司
  • 我买了一个备案网站 可是公司注销了seo前线
  • 安徽住房和城乡建设厅网站首页如何使用免费b站推广网站
  • 哪个网站可以做任务页面seo优化
  • 运维工程师可以自学吗公司seo是指什么意思
  • 男女做羞羞漫画网站百度广告推广怎么收费了
  • 品牌设计得公司曲靖seo建站
  • 2345网址导航app上海seo博客
  • 网站上传百度多久收录seo网站推广优化就找微源优化
  • 现在ui做的比较好的网站百度收录接口
  • wordpress is admin武汉seo优化代理
  • 动态网站登录加注册怎么做百度网盟官网
  • 东莞塘厦网站制作永久免费客服系统软件
  • 南京网站建设咨询百度广告怎么投放
  • 网站开发用到的编程南宁百度推广排名优化
  • 网站上传文件功能实现营销的方法和技巧
  • 电商网站的在线客服怎么做好搜搜索
  • 深圳网站建设网络推广盛大游戏优化大师