145. Binary Tree Postorder Traversal

二元樹的後序走訪

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    private $result = [];
    /**
     * @param TreeNode $root
     * @return Integer[]
     */
    function postorderTraversal($root) {
        $this->postorder($root);

        return $this->result;
    }

    private function postorder($root) {
        if ($root === null){
            return null;
        }
        
        $this->postorder($root->left);
        $this->postorder($root->right);
        $this->result[] = $root->val;
    }
}

Last updated