首页 > 其他 > 详细

Leetcode#144Binary Tree Preorder Traversal

时间:2015-05-21 06:46:17      阅读:156      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!