lc144-给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。


//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

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

    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null){
            return results;
        }
        results.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return results;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

posted @ 2023-02-01 14:19  小傻孩丶儿  阅读(21)  评论(0编辑  收藏  举报