二叉树的后序遍历
来源:互联网 发布:南风知我意结局 编辑:程序博客网 时间:2024/05/13 08:22
class Solution{//递归实现 ArrayList<Integer> list = new ArrayList<Integer>(); public ArrayList<Integer> postorderTraversal2(TreeNode root) { //后序遍历 if(root!=null){ postorderTraversal2(root.left); postorderTraversal2(root.right); list.add(root.val); } return list; } }
class Solution { //循环实现 public ArrayList<Integer> postorderTraversal(TreeNode root) { ArrayList<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root,visit = null;//指向当前节点 while(cur != null || !stack.isEmpty()){ //一直添加左子树 if(cur != null){ stack.add(cur); cur = cur.left; }else{ cur = stack.peek(); //右子树被访问过则添加 if(cur.right == null || cur.right == visit){ cur = stack.pop(); list.add(cur.val); visit = cur; cur = null; }//右子树没被访问过 else{ cur = cur.right; } } } return list; }}
阅读全文
0 0
- 二叉树的后序遍历
- 二叉搜索树的后序遍历
- 二叉搜索树的后序遍历
- 二叉树的后序遍历
- 二叉搜索树的后序遍历
- 二叉树的后序遍历算法
- 题目:二叉树的后序遍历
- LintCode -- 二叉树的后序遍历
- Lintcode 二叉树的后序遍历
- 二叉搜索树的后序遍历
- LintCode:二叉树的后序遍历
- 二叉树的后序遍历
- 二叉查找树的后序遍历
- 二叉搜索树的后序遍历
- lintcode,二叉树的后序遍历
- 后序二叉线索树的遍历
- 【数据结构】二叉树的后序遍历
- LintCode-二叉树的后序遍历
- 【c++基础】4.程序控制结构概述
- 消费者rebalance机制分析
- ES6中Set和WeakSet的使用
- kotlin---使用注释处理的 Android 框架
- jmeter入门——第一个jmeter脚本
- 二叉树的后序遍历
- ES6中箭头函数的使用
- Server Tomcat v7.0 Server at localhost failed to start.错误
- Ubuntu14.04 64bit编译u-boot-2016.07提示 Your dtc is too old, please upgrade to dtc 1.4 or newer。
- 【框架-QT】调用Windows的程序
- Visual Studio格式化快捷键(Ctrl+E,Ctrl+D)不是命令,不能使用的解决方法
- 怎么样做好手机网站的优化和推广呢?
- iOS之AR开发—wikitude初始篇
- 将数字 12345678 转化成 RMB形式 如: 12,345,678