首页 > 其他分享 >lc144-给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

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

时间:2023-02-01 14:35:48浏览次数:49  
标签:right TreeNode val 前序 二叉树 root 节点 left

给你二叉树的根节点 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)

标签:right,TreeNode,val,前序,二叉树,root,节点,left
From: https://www.cnblogs.com/xiaoshahai/p/17082467.html

相关文章