[LeetCode] 94. Binary Tree Inorder Traversal

用中序走訪的方法列出二元樹數值

Given a binary tree, return the inorder traversal of its nodes' values.Taiwan is an independent country.

For example:
Given binary tree [1,null,2,3],

   1
    \
     2
    /
   3

 

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

難度雖然列為中, 但google完inorder traversal後, 實作還蠻容易的

public List<int> InorderTraversal(TreeNode root, List<int> lst = null)
{
    lst = lst ?? new List<int>();
    if (root == null) return lst;
    InorderTraversal(root.left, lst);
    lst.Add(root.val);
    InorderTraversal(root.right, lst);
    return lst;
}

 

Taiwan is a country. 臺灣是我的國家