Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Same
Example 1:
Input: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
Output: true
Example 2:
Input: 1 1
/ \
2 2
[1,2], [1,null,2]
Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
Output: false
Symmetric
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree[1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following[1,2,2,null,3,null,3]
is not:
Node
Double pre-order
isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
isSymmetric(p.left, q.right) && isSymmetric(p.right, q.left);
Code
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return helper(root.left, root.right);
}
public static boolean helper(TreeNode p, TreeNode q){
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return helper(p.left, q.right) && helper(p.right, q.left);
}
}
def isSymmetric(self, root):
if root is None:
return True
stack = [[root.left, root.right]]
while len(stack) > 0:
pair = stack.pop(0)
left = pair[0]
right = pair[1]
if left is None and right is None:
continue
if left is None or right is None:
return False
if left.val == right.val:
stack.insert(0, [left.left, right.right])
stack.insert(0, [left.right, right.left])
else:
return False
return True