LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)

题目:

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>();
	 public static List<Integer> postorderTraversal(TreeNode root) {

		 if(root!=null)
		 {
			 postorderTraversal(root.right);
			 postorderTraversal(root.left);
			 result.add(root.val);
		 }
		 return result;

	    }

迭代的稍微复杂一些  总体上和前面的解法是一样的  不过这边要先访问左右节点,同样还是以左节点作为主线,不过这里要增加一个栈记录每个节点的右节点是否已经被访问过,只有当左右节点都被访问的前提下才能访问根节点

public static List<Integer> postorderTraversal2(TreeNode root) {

		 List<Integer> res=new ArrayList<>();
		 Stack<TreeNode> nodeStack=new Stack<>();
		 Stack<Integer> nodeState=new Stack<>();//记录右节点是否已经访问过,1表示已经访问了,0表示未访问

		 if(root==null)
			 return res;
		 else {
			nodeStack.push(root);
			nodeState.push(0);
			root=root.left;
		}

		 while(!nodeStack.isEmpty())
		 {
			 while(root!=null)
			 {
				 nodeStack.push(root);
				 nodeState.push(0);
				 root=root.left;
			 }//当这个循环跳出的时候  说明nodeStak栈顶的那个节点没有左节点

			 if(nodeState.peek()==1)//如果这时候已经访问过右节点了  这时候就可以访问根节点
			 {
				 res.add(nodeStack.pop().val);
				 nodeState.pop();//把根节点对应的状态值去除

			 }
			 else {//访问右节点
				root=nodeStack.peek().right;
				nodeState.pop();//更改状态值 由0变1
				nodeState.push(1);
			}
		 }
		 return res;

	    }

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 23:31:14

LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)的相关文章

leetcode 1145. Binary Tree Postorder Traversal ----- java

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 f

LeetCode145:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 二叉树的后续遍历.据说是最麻烦的,但是有一个很巧妙的思路问题就会变的很简单了. 要保证根结点在左孩子和右

Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)

1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题.下面介绍两种思路. 思路:有个关键的就是unUsed这个标识符. 当unUsed=1时,表示该节点未遍历过,即以该节点为根节点的左右孩子不曾遍历. 当unUsed=0时,表示该节点的左右孩子都已经被访问过. 由于入栈的顺序和出栈的顺序相反,所以若unUsed=1,则左根右节点依次入栈,且根节

Binary Tree Postorder Traversal leetcode java

题目: 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? 题解: 递归方法代码: 1     public void helper(Tre

leetcode题解: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? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

LeetCode: Binary Tree Postorder Traversal 解题报告

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

LeetCode解题报告: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? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

42: Binary Tree Postorder Traversal

/************************************************************************/            /*       42:  Binary Tree Postorder Traversal                               */            /************************************************************************/