P16331 [GeeksforGeeks] Foldable binary tree
普及
题目描述

Given a binary tree, check if the tree can be folded or not. A tree can be folded if left and right subtrees of the tree are structure wise mirror image of each other. An empty tree is considered as foldable.
Consider the below trees:
(a) and (b) can be folded.
(c) and (d) cannot be folded.
(a)
10
/ \
7 15
\ /
9 11
(b)
10
/ \
7 15
/ \
9 11
(c)
10
/ \
7 15
/ /
5 11
(d)
10
/ \
7 15
/ \ /
9 10 12

Example 1:
Input:
10
/ \
7 15
/ \ / \
N 9 11 N
Output:Yes
Explaination:Structure of every left and right subtree are same.
Example 2:
Input:
10
/ \
7 15
/ \ / \
5 N 11 N
Output: No
Explaination: 7's left child is not NULL and right child is NULL. That's why the tree is not foldable.
Your Task:
The task is to complete the function isFoldable() that takes root of the tree as input and returns true or false depending upon whether the tree is foldable or not.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
0 <= n <= 10^{3}
1 <= data of node <= 10^{4}

输入格式

见题面

输出格式

见题面

样例输入
10 / \ 7 15 / \ / \ N 9 11 N
样例输出
Yes