[leetcode]Binary Tree Preorder Traversal-先序遍历 java

注意以下几点

1. 输入为null时,返回空list,题目没有说

 

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null){
            return new ArrayList<Integer>();
        }
        List<Integer> result = new ArrayList<Integer>();
        result.add(root.val);
        if(root.left!=null){
            result.addAll(preorderTraversal(root.left));
        }
        if(root.right != null){
            result.addAll(preorderTraversal(root.right));
        }
        return result;    
    }
}

 

你可能感兴趣的:(LeetCode,算法)