Skip to content

Latest commit

 

History

History
 
 

preorderTraversal

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

二叉树的前序遍历

LeetCode地址

把中序遍历的代码copy过来,直接改:

public class Solution {

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        helper(root, res);
        return res;
    }

    private void helper(TreeNode root, List<Integer> res) {
        if (root != null) {
            res.add(root.val);
            if (root.left != null) helper(root.left, res);
            if (root.right != null) helper(root.right, res);
        }
    }
}