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].


就是二叉树的后序遍历。


思路:后序遍历的顺序- 左右根 ,直接使用递归实现就可以了。


实现代码:



/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public IList<int> PostorderTraversal(TreeNode root) 
    {
        var result = new List<int>();
    	Travel(root, ref result);
    	
    	return result;
    }
    
    private void Travel(TreeNode node, ref List<int> result)
	{
    	if(node == null){
			return;
		}
		
		Travel(node.left, ref result);
		Travel(node.right, ref result);
		result.Add(node.val);
    }


}


你可能感兴趣的:(LeetCode -- Binary Tree Postorder Traversal)