Binary Tree Postorder Traversal
来源:互联网 发布:电脑无损音乐播放软件 编辑:程序博客网 时间:2024/05/17 23:01
- 题目 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.
给出一棵二叉树,返回其节点值的后序遍历。 - 算法
递归:先遍历左节点,遍历右节点,访问跟节点
public ArrayList<Integer> postorderTraversal(TreeNode root) { // write your code here ArrayList<Integer> res = new ArrayList<Integer>(); if (root == null) { return res; } helper(root, res); return res; } public void helper(TreeNode root, ArrayList<Integer> res) { if (root != null) { helper(root.left, res); //遍历左节点 helper(root.right, res); //遍历右节点 res.add(root.val); //访问根节点 } }
非递归方法比较复杂,我们用cur表示当前节点,用pre表示前一个遍历的节点,步骤如下
- 当pre的左节点或右节点为cur,表示在向下遍历,如果cur有左节点,则左节点进栈,如果有右节点,右节点进栈,如果cur为叶节点,则直接访问并出栈
- 当cur的左节点为pre时,说明在回溯,根据后续遍历准则,如果cur有右节点,则右节点进栈,如果没有右节点,则访问并出栈
- 当cur的右节点为pre时,说明左右节点都访问了,访问cur并出栈
public ArrayList<Integer> postorderTraversal(TreeNode root) { // write your code here ArrayList<Integer> res = new ArrayList<Integer>(); if (root == null) { return res; } TreeNode pre = null; LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); stack.push(root); while(!stack.isEmpty()) { // 当栈不空时循环 TreeNode cur = stack.peek(); // 当前元素为栈顶元素 if (pre == null || pre.left == cur || pre.right == cur) { //第一个元素进栈时pre为空 if (cur.left != null) { stack.push(cur.left); // 右节点进栈 } else if (cur.right != null) { stack.push(cur.right); //左节点进栈 } else { res.add(cur.val); //为叶节点,访问并弹栈 stack.pop(); } } else if (cur.left == pre && cur.right != null) { stack.push(cur.right); // 右节点进栈 } else { // pre为cur右节点,或右节点为空 res.add(cur.val); stack.pop(); } pre = cur; // 最后交换 } return res;
其实上面代码还有一中写法
public ArrayList<Integer> postorderTraversal(TreeNode root) { // write your code here ArrayList<Integer> res = new ArrayList<Integer>(); if (root == null) { return res; } TreeNode pre = null; LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); while (root != null || !stack.isEmpty()) { if (root != null) { stack.push(root); root = root.left; } else { TreeNode peekNode = stack.peek(); if (peekNode.right != null && pre != peekNode.right) {// 当右节点不为空或没访问过 root = peekNode.right; } else { res.add(peekNode.val); stack.pop(); pre = peekNode; } } } return res; }
0 0
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- mogodb的使用及自制工具类
- 在子线程中利用主线程的Handler的post()方法
- C++ char* 的若干问题之二:char*作为形参能否影响实参的值
- .index() :gt()Selector :lt()Selector
- d3.js学习笔记
- Binary Tree Postorder Traversal
- chrome浏览器开发者工具的使用
- 深入理解css3的border边框
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- 通过bat批处理,向没有root的手机pull和push文件
- c#滚动条控件的应用
- oj-14-矩阵对角线和反对角线的和
- leetcode 链表 排序
- 【记录】sql server 2016 数据字典