Flip a binary tree.
Input:
Output:
This problem is a classic binary tree operation. It involves recursively traversing from the root node, flipping from the leaf nodes. If the current node is root
, then only need to continue swapping the positions of the two subtrees to complete the flip. Firstly, check if the node exists, if not, return an empty node directly. Then recursively invert the left subtree and right subtree, and then define a destructuring assignment operation (ES6 allows to extract values from arrays and objects based on certain patterns, and assign values to variables, this is called destructuring assignment). Swap the position of the left subtree and right subtree, similar to post-order recursive traversal. Because the recursive operation is continuously flipping from the leaf nodes, finally return the root node.