首页 > 其他 > 详细

Leet-code144. Binary Tree Preorder Traversal

时间:2016-03-13 17:36:42      阅读:145      评论:0      收藏:0      [点我收藏+]

这是一道将二叉树先序遍历,题目不难,采用深搜

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
   public static List<Integer> resultlist = new ArrayList<Integer>();

    public static void dfs (TreeNode root ,boolean flag ) {
        if(flag==true)
        {
            resultlist.clear();
        }
        if(root==null)
        {
            return ;
        }

            resultlist.add(root.val);

        if(root.left!=null)
        {
            dfs(root.left,false);
        }
        if(root.right!=null)
        {
            dfs(root.right,false);
        }
        
    }


    public static List<Integer> preorderTraversal(TreeNode root ) {
        dfs(root,true);
        return resultlist;
    }

}

 

Leet-code144. Binary Tree Preorder Traversal

原文:http://www.cnblogs.com/ren-jie/p/5272388.html

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