码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode -- Binary Tree Preorder Traversal

时间:2015-10-17 01:54:41      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:


Given a binary tree, return the preorder traversal of its nodes‘ values.


For example:
Given binary tree {1,#,2,3},
   1
    \
     2
    /
   3
return [1,2,3].


思路:


题目很直白,就是树的先序遍历。直接实现就可以了。


实现代码:





/**
 * 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> PreorderTraversal(TreeNode root) 
    {
        var ret = new List<int>();
    	Travel(root, ref ret);
    	
    	return ret;
    }


private void Travel(TreeNode current, ref List<int> result)
{
	if(current == null){
		return ;
	}
	result.Add(current.val);
	
	Travel(current.left, ref result);
	Travel(current.right, ref result);
}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode -- Binary Tree Preorder Traversal

标签:

原文地址:http://blog.csdn.net/lan_liang/article/details/49188153

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!