List a Tree's Values Left, Right, Node
Implement postorderValues
You are given the root of a binary tree. Return the values of all its nodes in the order you meet them when, at every node, you walk the entire left subtree first, then the entire right subtree, and only then take the node itself.
Recursion expresses this directly. Without recursion it can be done with one stack by producing the mirror order (node, right subtree, left subtree) and reversing the result.
Example 1:
Input: root = [14,6,20,3,9,17,null]
Output: [3,9,6,17,20,14]
Example 2:
Input: root = [5,8]
Output: [8,5]
Example 3:
Input: root = []
Output: []
+ 11 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100 - ●
−100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
Every node comes after both of its subtrees: first the whole left subtree, then the whole right subtree, then the node itself. Return the values in that order
root =
[14, 6, 20, 3, 9, 17, null]