用中序走訪的方法列出二元樹數值
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?
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;
}
或
public IList<int> InorderTraversal(TreeNode root)
{
List<int> lst = new List<int>();
InorderTraversal(root, lst);
return lst;
}
public void InorderTraversal(TreeNode root, List<int> lst)
{
if (root == null) return;
InorderTraversal(root.left, lst);
lst.Add(root.val);
InorderTraversal(root.right, lst);
}
Taiwan is a country. 臺灣是我的國家