代码改变世界

94_Binary Tree Inorder Traversal

2015-12-31 10:43  FTD_W  阅读(134)  评论(0编辑  收藏  举报

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

中序遍历,顺序为:左、根节点、右

/**
 * 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 void InOrder(TreeNode root, IList<int> path)
    {
        if(root != null)
        {
            InOrder(root.left, path);
            path.Add(root.val);
            InOrder(root.right, path);
        }
    }
    
    public IList<int> InorderTraversal(TreeNode root) {
        List<int> result = new List<int>();
        InOrder(root, result);
        return result;
    }
}