Home > Backend Development > PHP Tutorial > . Binary Tree Postorder Traversal

. Binary Tree Postorder Traversal

王林
Release: 2024-08-26 08:30:31
Original
591 people have browsed it

145. Binary Tree Postorder Traversal

Difficulty: Easy

Topics: Stack, Tree, Depth-First Search, Binary Tree

Given the root of a binary tree, return the postorder traversal of its nodes' values.

Example 1:

. Binary Tree Postorder Traversal

  • Input: root = [1,null,2,3]
  • Output: [3,2,1]

Example 2:

  • Input: root = []
  • Output: []

Example 3:

  • Input: root = [1]
  • Output: [1]

Constraints:

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Solution:

We can use an iterative approach with a stack. Postorder traversal follows the order: Left, Right, Root.

Let's implement this solution in PHP: 145. Binary Tree Postorder Traversal

val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}
/**
* @param TreeNode $root
* @return Integer[]
*/
function postorderTraversal($root) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example usage:

// Example 1
$root1 = new TreeNode(1);
$root1->right = new TreeNode(2);
$root1->right->left = new TreeNode(3);
print_r(postorderTraversal($root1)); // Output: [3, 2, 1]

// Example 2
$root2 = null;
print_r(postorderTraversal($root2)); // Output: []

// Example 3
$root3 = new TreeNode(1);
print_r(postorderTraversal($root3)); // Output: [1]
?>




Explanation:

  • TreeNode Class: The TreeNode class defines a node in the binary tree, including its value, left child, and right child.

  • postorderTraversal Function:

    • We initialize an empty result array and a stack.
    • We use a while loop that continues as long as either the stack is not empty or the current node is not null.
    • If the current node is not null, we push it onto the stack and move to its left child.
    • If the current node is null, we check the top node of the stack. If it has a right child that we haven't visited yet, we move to the right child. Otherwise, we add the node's value to the result array and pop it from the stack.

This iterative approach simulates the recursive postorder traversal without using system recursion, making it more memory-efficient.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

  • LinkedIn
  • GitHub

The above is the detailed content of . Binary Tree Postorder Traversal. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template