g0101_0200.s0101_symmetric_tree.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0101_symmetric_tree;
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search
// #Tree #Binary_Tree #Data_Structure_I_Day_11_Tree #Level_2_Day_15_Tree
// #Big_O_Time_O(N)_Space_O(log(N)) #2022_06_22_Time_0_ms_(100.00%)_Space_42.3_MB_(46.67%)
import com_github_leetcode.TreeNode;
/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/**
* 101 - Symmetric Tree\.
*
* Easy
*
* Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
*
* **Example 1:**
*
* ![](https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg)
*
* **Input:** root = [1,2,2,3,4,4,3]
*
* **Output:** true
*
* **Example 2:**
*
* ![](https://assets.leetcode.com/uploads/2021/02/19/symtree2.jpg)
*
* **Input:** root = [1,2,2,null,3,null,3]
*
* **Output:** false
*
* **Constraints:**
*
* * The number of nodes in the tree is in the range `[1, 1000]`.
* * `-100 <= Node.val <= 100`
*
* **Follow up:** Could you solve it both recursively and iteratively?
**/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return helper(root.left, root.right);
}
private boolean helper(TreeNode leftNode, TreeNode rightNode) {
if (leftNode == null || rightNode == null) {
return leftNode == null && rightNode == null;
}
if (leftNode.val != rightNode.val) {
return false;
}
return helper(leftNode.left, rightNode.right) && helper(leftNode.right, rightNode.left);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy