Binary Tree Postorder Traversal 二叉树的后序遍历

地址:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/

题意就是完成二叉树的后序遍历,我们知道如果使用递归进行二叉树后序遍历将是非常简单的事情。

public class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer > ans = new ArrayList<>();
        Traversal(root, ans);
        return ans;
    }
    public void Traversal(TreeNode root,List<Integer > ans){
    	if(root!=null){
    		Traversal(root.left, ans);
    		Traversal(root.right, ans);
    		ans.add(root.val);
    	}
    }
}

但是这里需要进行非递归的后序遍历,在这篇博文中已经写过中序遍历的非递归程序和一些经典二叉搜索树的面试题:http://blog.csdn.net/huruzun/article/details/21799441 用C++去实现的。

非递归后序遍历的算法思想:要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。如果P不存在左孩子和右孩子,则可以直接访问它;或者P存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。

public class Solution {
       public List<Integer> postorderTraversal(TreeNode root) {
    	List<Integer > ans = new ArrayList<>();
    	if(root == null){
    		return ans;
    	}
    	Stack<TreeNode> st = new Stack<>();
    	st.push(root);
    	TreeNode pre = null;
    	TreeNode cur;
    	while(st.size()!=0){
    		cur = st.peek();
    		if((cur.left==null&&cur.right==null)||(pre!=null && ((pre==cur.left)||(pre==cur.right)))){
    			ans.add(cur.val);
    			st.pop();
    			pre = cur;
    		}else {
				if(cur.right!=null){
					st.add(cur.right);
				}
				if(cur.left!=null){
					st.add(cur.left);
				}
			}
    	}
    	return ans;
    }

}

最后把所有测试代码也贴出来

package 二叉搜索树遍历;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer > ans = new ArrayList<>();
        Traversal(root, ans);
        return ans;
    }
    public void Traversal(TreeNode root,List<Integer > ans){
    	if(root!=null){
    		Traversal(root.left, ans);
    		Traversal(root.right, ans);
    		ans.add(root.val);
    	}
    }
    public List<Integer> postorderTraversal2(TreeNode root) {
    	List<Integer > ans = new ArrayList<>();
    	if(root == null){
    		return ans;
    	}
    	Stack<TreeNode> st = new Stack<>();
    	st.push(root);
    	TreeNode pre = null;
    	TreeNode cur;
    	while(st.size()!=0){
    		cur = st.peek();
    		if((cur.left==null&&cur.right==null)||(pre!=null && ((pre==cur.left)||(pre==cur.right)))){
    			ans.add(cur.val);
    			st.pop();
    			pre = cur;
    		}else {
				if(cur.right!=null){
					st.add(cur.right);
				}
				if(cur.left!=null){
					st.add(cur.left);
				}
			}
    	}
    	return ans;
    }

    // 这里root 是传值,所以必须返回root值使得树能建立。
    public TreeNode insert(TreeNode root,int val){
    	if(root == null){
    		root = new TreeNode(val);
    		root.left = null;
    		root.right = null;
    		return root;
    	}
    	else {
			if(root.val<val){
				root.right = insert(root.right, val);
			}else if(root.val>val){
				root.left =  insert(root.left, val);
			}else {

			}
		}
    	return root;
    }
	public static void main(String[] args) {
		int []num = {1,2};
		TreeNode root = null;
		Solution s = new Solution();
		for(int i=0;i<num.length;i++){
			root = s.insert(root, num[i]);
		}
		System.out.println(s.postorderTraversal(root));
		System.out.println(s.postorderTraversal2(root));
	}

}

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) {
		val = x;
	}
}
时间: 2024-10-26 07:53:36

Binary Tree Postorder Traversal 二叉树的后序遍历的相关文章

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] 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? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序

145 Binary Tree Postorder Traversal 二叉树的后序遍历

给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解决吗?详见:https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 方法一:递归 /** * Definition for a binary tree node. * struct TreeNode { *

[LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? > read

lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历

题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val

[LeetCode] 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? http://www.cnblogs.com/dolphin0520/archive/201

leetcode 94.Binary Tree Inorder Traversal 二叉树的中序遍历

递归算法C++代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> in

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? Show Tags #include<iostream> #include<vec

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig