Lettcode_145_Binary Tree Postorder Traversal

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876769


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)考虑到用递归比较简单,本文使用递归的思想进行解决,由于比较简单这里不累赘,详见下方代码。

(3)希望本文对你有所帮助。



算法代码实现如下:

/**
 * @author liqq
 */
public List<Integer> PostorderTraversal(TreeNode root) {
	List<Integer> result = new LinkedList<Integer>();
	if (root != null) {
		Post_order(result, root.left);
		Post_order(result, root.right);
		result.add(root.val);
	}
	return result;
}

private void Post_order(List<Integer> result, TreeNode curr) {
	if (curr != null) {
		Post_order(result, curr.left);
		Post_order(result, curr.right);
		result.add(curr.val);
	}
}



你可能感兴趣的:(java,LeetCode,算法,二叉树)