avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-144-Binary Tree Preorder Traversal
文章

Leetcode-144-Binary Tree Preorder Traversal

Posted 2022-07-18
4~5 min read

Binary Tree Preorder Traversal

leetcode: https://leetcode.com/problems/binary-tree-preorder-traversal/

Description:

Given the root of a binary tree, return the preorder traversal of its nodes’ values.

Idea:

the order of prerder is parent node- left node-right node(start from the root)

**Step1: **create the method and use recurision to traverse the whole tree.

**Step2: **add parent node firstly, then move the parent node to it’s left child,then in the recurison, when left subtree finished, make the node move to the right child and traverse the right substree.

Code:

/**
 * 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 {
    public List<Integer> preorderTraversal(TreeNode root) {
        //create the result list
        List<Integer> result = new ArrayList<Integer>();
        preorder(root,result);
        return result;
    }
    public void preorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        //in the preorder, firstly add the parent node from the root.
        result.add(root.val);
        //then add the left child secondly, at the same time, in the recurision, we move the root to it's left child
        preorder(root.left, result);
        //then add the right child thirdly, at the same time, in the recurision, we move the root to it's left child
        preorder(root.right, result);
    }
}
Leetcode
Leetcode Binary Tree
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leetcode-347-Top K Frequent Elements

NEWER

Leetcode-94-Binary Tree Inorder Traversal

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents