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

Leetcode#144Binary Tree Preorder Traversal

时间:2015-05-21 06:49:26      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:example   return   public   null   

Binary Tree Preorder Traversal

 Total Accepted: 67121 Total Submissions: 185051My Submissions

Question Solution 


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


分析:先序遍历树中的节点,采用递归的方法


public class Solution {

    

    List<Integer> x=new ArrayList<Integer>();

    

    void preTra(TreeNode r) {

            if(r!=null)

            {

                x.add(r.val);

                preTra(r.left);

                preTra(r.right);

            }

                

    }

    

    public List<Integer> preorderTraversal(TreeNode root) {

            preTra(root);

            return x;

    }

}


Leetcode#144Binary Tree Preorder Traversal

标签:example   return   public   null   

原文地址:http://7061299.blog.51cto.com/7051299/1653309

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