题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. 解题: 递归的还是和前面中序和先序一样.仅仅是交换一下顺序而已 public static List<Integer> result=new ArrayList<Integer>(); publ…
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 求后序遍历,要求不使用递归. 使用栈,从后向前添加. /** * Definition…
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. Given a binary tree, return the postorder traversal of its nodes…
1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题.下面介绍两种思路. 思路:有个关键的就是unUsed这个标识符. 当unUsed=1时,表示该节点未遍历过,即以该节点为根节点的左右孩子不曾遍历. 当unUsed=0时,表示该节点的左右孩子都已经被访问过. 由于入栈的顺序和出栈的顺序相反,所以若unUsed=1,则左根右节点依次入栈,且根节…
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; vector<int> postorderTraversal(TreeNode* root) { if(root == NULL) return res; postorderTraversal(root ->left); postorderTraversal(root ->right);…
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree Postorder Traversal 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? Java 实现 Iterative Solution import java.util.LinkedList; impo…
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且先存储左节点,再存储右节点,就变成了逐行打印 class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> result; if(root == NULL) return res…
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively…
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tr…
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归法 /**…