question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
contain-virus | Efficient JS solution (Beat 100% both time and memory) | efficient-js-solution-beat-100-both-time-g46j | \n\n# Intuition\nFirst BFS, then BFS, and BFS right after that. Finally, BFS.\n*Edit: I forgot that we also need BFS.\n\n# Complexity\n- Time complexity: O((mn) | CuteTN | NORMAL | 2023-09-24T11:28:35.855727+00:00 | 2023-09-24T11:28:35.855758+00:00 | 27 | false | \n\n# Intuition\nFirst BFS, then BFS, and BFS right after that. Finally, BFS.\n*Edit: I forgot that we also need BFS.\n\n# Complexity\n- Time complexity: $$O((mn)^2)$$\n- Space complexity: $$O(mn)$$\n\n# Cod... | 0 | 0 | ['Breadth-First Search', 'Queue', 'JavaScript'] | 0 |
contain-virus | ONLY GOD KNOWS | only-god-knows-by-hrtoimukra-h05h | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | hrtoimukra | NORMAL | 2023-09-01T06:50:16.980176+00:00 | 2023-09-01T06:50:16.980197+00:00 | 61 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['C++'] | 0 |
contain-virus | Easiest Solution | easiest-solution-by-kunal7216-0gsu | \n\n# Code\njava []\nclass Solution {\n \n private static final int[][] DIR = new int[][]{\n {1, 0}, {-1, 0}, {0, 1}, {0, -1}\n };\n \n pu | Kunal7216 | NORMAL | 2023-08-28T14:50:39.970699+00:00 | 2023-08-28T14:50:39.970729+00:00 | 142 | false | \n\n# Code\n``` java []\nclass Solution {\n \n private static final int[][] DIR = new int[][]{\n {1, 0}, {-1, 0}, {0, 1}, {0, -1}\n };\n \n public int containVirus(int[][] isInfected) {\n int m = isInfected.length, n = isInfected[0].length;\n int ans = 0;\n \n while( tr... | 0 | 0 | ['C++', 'Java', 'Python3'] | 0 |
contain-virus | C++ faster than 92% | c-faster-than-92-by-ialight-cxqk | First get the new infection count for every region without spreading the infection. Also save the infected area cells for spreading the infection later. To coun | ialight | NORMAL | 2023-08-06T16:28:00.184064+00:00 | 2023-08-06T16:28:00.184089+00:00 | 20 | false | First get the new infection count for every region without spreading the infection. Also save the infected area cells for spreading the infection later. To count the possibly infected cells for every region, I am using different area/region code for visited matrix to not skip the non-infected cells. \n\nAfter that get ... | 0 | 0 | ['Depth-First Search'] | 0 |
contain-virus | C++ Solution which is easy to understand | c-solution-which-is-easy-to-understand-b-mexm | Intuition\n The objective is to identify the region that affects the maximum number of blocks and construct walls around it. This process is repeated until th | next_big_thing | NORMAL | 2023-07-22T19:59:30.632428+00:00 | 2023-07-22T20:03:11.395600+00:00 | 93 | false | # Intuition\n The objective is to identify the region that affects the maximum number of blocks and construct walls around it. This process is repeated until there are no remaining open regions\n\n# Approach\n\n1. find region which impacts maximum blocks\n2. contruct walls around it and deidentify the region and add ... | 0 | 0 | ['Greedy', 'Depth-First Search', 'C++'] | 0 |
contain-virus | Detailed and clear solution | detailed-and-clear-solution-by-mdakram28-db9m | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | mdakram28 | NORMAL | 2023-04-14T00:24:48.793514+00:00 | 2023-04-14T00:24:48.793546+00:00 | 104 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Python3'] | 0 |
contain-virus | Simple Explained Solution | simple-explained-solution-by-darian-cata-yuic | \n\nclass Solution {\npublic:\n int m, n;\n int ans = 0;\n \n // -1 means disinfected\n int color = 2;\n \n // for perimeter calculation we | darian-catalin-cucer | NORMAL | 2023-02-12T10:28:55.872303+00:00 | 2023-02-12T10:28:55.872348+00:00 | 237 | false | \n```\nclass Solution {\npublic:\n int m, n;\n int ans = 0;\n \n // -1 means disinfected\n int color = 2;\n \n // for perimeter calculation we cannot go to cells marked -1 but we can goto all other cells\n int perimeter = 0;\n \n // we can go to cells marked as 0 only\n // this will sto... | 0 | 0 | ['C++', 'Scala', 'Ruby', 'Kotlin', 'JavaScript'] | 0 |
contain-virus | [Golang] DFS easy to understand | golang-dfs-easy-to-understand-by-user044-b833 | go\nfunc containVirus(isInfected [][]int) int {\n m, n := len(isInfected), len(isInfected[0])\n var res int\n for {\n // Let\'s do a depth first search an | user0440H | NORMAL | 2023-01-10T12:57:23.268070+00:00 | 2023-01-10T12:57:23.268115+00:00 | 110 | false | ```go\nfunc containVirus(isInfected [][]int) int {\n m, n := len(isInfected), len(isInfected[0])\n var res int\n for {\n // Let\'s do a depth first search and find the region that spread the most cells\n // (i.e) needs more walls to be built\n visited := make([][]bool, m)\n for i := 0; i < m; i++ {\n ... | 0 | 0 | ['Go'] | 0 |
contain-virus | Python Short and Easy to Understand Solution | python-short-and-easy-to-understand-solu-2vyd | \n\nclass Solution:\n def containVirus(self, isInfected: List[List[int]]) -> int:\n walls_needed = 0\n\n # identify infected and uninfected cel | kensverse | NORMAL | 2022-12-27T14:36:52.045026+00:00 | 2022-12-27T14:36:52.045057+00:00 | 300 | false | \n```\nclass Solution:\n def containVirus(self, isInfected: List[List[int]]) -> int:\n walls_needed = 0\n\n # identify infected and uninfected cells\n infected = [(i, j) for i in range(len(isInfected)) for j in range(len(isInfected[0])) if isInfected[i][j] == 1]\n uninfected = [(i, j) for... | 0 | 0 | ['Python3'] | 0 |
contain-virus | [Python] DFS and simulation. explained | python-dfs-and-simulation-explained-by-w-p5he | (1) DFS to find all the infected regions\n (2) Pick the region that will infect the largetest number of cells in the next day, and build a wall around it\n (3) | wangw1025 | NORMAL | 2022-12-21T23:58:34.557894+00:00 | 2022-12-22T00:00:44.359008+00:00 | 208 | false | * (1) DFS to find all the infected regions\n* (2) Pick the region that will infect the largetest number of cells in the next day, and build a wall around it\n* (3) Update the infected regions\nthe cells that are in the wall is updated with "controlled" state (i.e., value 2)\nthe cells that is on the boundary of uncontr... | 0 | 0 | ['Depth-First Search', 'Python3'] | 0 |
contain-virus | Python Object Oriented Solution - Easy to understand | python-object-oriented-solution-easy-to-9f506 | Intuition\n Describe your first thoughts on how to solve this problem. \nThis is Python version:\nFor explanation please refer CPP Version\nCredit goes to: rai0 | hidden_hive | NORMAL | 2022-12-20T07:11:43.939538+00:00 | 2022-12-20T15:18:42.332416+00:00 | 245 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is Python version:\nFor explanation please refer [CPP Version](https://leetcode.com/problems/contain-virus/solutions/847507/cpp-dfs-solution-explained/)\nCredit goes to: [rai02](https://leetcode.com/rai02/)\n\n# Approach\n<!-- Descri... | 0 | 0 | ['Depth-First Search', 'Python3'] | 0 |
contain-virus | Just a runnable solution | just-a-runnable-solution-by-ssrlive-47g0 | Code\n\nstruct MySolution {\n infected: Vec<Vec<i32>>,\n n: usize,\n m: usize,\n c: i32,\n mx: usize,\n w: i32,\n r: i32,\n ans: i32,\n | ssrlive | NORMAL | 2022-12-14T09:31:56.215579+00:00 | 2022-12-14T09:31:56.215626+00:00 | 47 | false | # Code\n```\nstruct MySolution {\n infected: Vec<Vec<i32>>,\n n: usize,\n m: usize,\n c: i32,\n mx: usize,\n w: i32,\n r: i32,\n ans: i32,\n itr: i32,\n s: std::collections::HashSet<usize>,\n}\n\nimpl MySolution {\n fn new() -> Self {\n MySolution {\n infected: vec![ve... | 0 | 0 | ['Rust'] | 0 |
contain-virus | Simulation approach utilizing Union-Find to keep track of regions and BFS to expand a virus region. | simulation-approach-utilizing-union-find-6r9r | Intuition\nWe will perform an actual simulation of the virus infection.\n\n# Approach\nThe logical approach isn\'t a complicated one, rather it\'s even presente | lynnlu | NORMAL | 2022-12-02T04:01:10.756232+00:00 | 2022-12-02T04:01:10.756258+00:00 | 157 | false | # Intuition\nWe will perform an actual simulation of the virus infection.\n\n# Approach\nThe logical approach isn\'t a complicated one, rather it\'s even presented in the problem statement:\n- Keep track of the virus regions\n- For each virus region, find the cells threatened by that region.\n - all these cells will... | 0 | 0 | ['Breadth-First Search', 'Union Find', 'Scala'] | 0 |
contain-virus | Contain Virus O(n*m*max(n,m)) || DFS || Leetcode Hard | contain-virus-onmmaxnm-dfs-leetcode-hard-1740 | Intuition\n Describe your first thoughts on how to solve this problem. \nBasically I am trying to traverse First ifected cell then dfs after that spreading the | kgstrivers | NORMAL | 2022-11-18T22:37:21.169682+00:00 | 2022-11-18T22:40:40.901191+00:00 | 241 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBasically I am trying to traverse First ifected cell then dfs after that spreading the infected case by adjacent untill I get the optimal answer,So that\'s why while(true) every time I chck that infected cell then dfs and it will repaetin... | 0 | 0 | ['Graph', 'Matrix', 'C++'] | 0 |
contain-virus | C++ dfs | c-dfs-by-tushrrrocks-fy2p | \n\nclass Solution {\npublic:\n vector<vector<int> > g;\n int n, m, c, mx, w, r, ans, itr;\n unordered_set<int> s;\n \n int dfs(int i, int j){\n | tushrrrocks | NORMAL | 2022-11-18T19:55:58.472995+00:00 | 2022-11-18T19:55:58.473035+00:00 | 139 | false | ```\n\nclass Solution {\npublic:\n vector<vector<int> > g;\n int n, m, c, mx, w, r, ans, itr;\n unordered_set<int> s;\n \n int dfs(int i, int j){\n if(i<0 || i>=n || j<0 || j>=m || g[i][j]!=1)\n return 0;\n int ans=0;\n if(i+1<n && g[i+1][j]==0){\n s.insert((i+1... | 0 | 0 | ['Depth-First Search', 'Graph', 'C', 'Matrix'] | 0 |
symmetric-tree | 🔥Easy Solutions in Java 📝, Python 🐍, and C++ 🖥️🧐Look at once 💻 | easy-solutions-in-java-python-and-c-look-zypw | Intuition\n Describe your first thoughts on how to solve this problem. \n To check if a binary tree is symmetric, we need to compare its left subtree and right | Vikas-Pathak-123 | NORMAL | 2023-03-13T00:22:59.481572+00:00 | 2023-03-13T00:22:59.481609+00:00 | 91,994 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n To check if a binary tree is symmetric, we need to compare its left subtree and right subtree. To do this, we can traverse the tree recursively and compare the left and right subtrees at each level. If they are symmetric, we continue the... | 848 | 5 | ['Depth-First Search', 'Binary Tree', 'Python', 'C++', 'Java'] | 31 |
symmetric-tree | Recursive and non-recursive solutions in Java | recursive-and-non-recursive-solutions-in-95ma | Recursive--400ms:\n\n public boolean isSymmetric(TreeNode root) {\n return root==null || isSymmetricHelp(root.left, root.right);\n }\n \n pri | lvlolitte | NORMAL | 2014-12-12T08:03:34+00:00 | 2018-10-20T15:12:41.559022+00:00 | 183,004 | false | Recursive--400ms:\n\n public boolean isSymmetric(TreeNode root) {\n return root==null || isSymmetricHelp(root.left, root.right);\n }\n \n private boolean isSymmetricHelp(TreeNode left, TreeNode right){\n if(left==null || right==null)\n return left==right;\n if(left.val!=right... | 770 | 8 | ['Stack', 'Recursion', 'Java'] | 102 |
symmetric-tree | Recursively and iteratively solution in Python | recursively-and-iteratively-solution-in-noyi3 | Basically, this question is recursively. Or we can say, the tree structure is recursively, so the recursively solution maybe easy to write:\n\nTC: O(b) SC: O(lo | wizcabbit | NORMAL | 2014-11-02T08:35:29+00:00 | 2018-10-22T04:12:23.448576+00:00 | 75,870 | false | Basically, this question is recursively. Or we can say, the tree structure is recursively, so the recursively solution maybe easy to write:\n\nTC: O(b) SC: O(log n)\n\n class Solution:\n def isSymmetric(self, root):\n if root is None:\n return True\n else:\n return self.isMirror(... | 344 | 4 | [] | 44 |
symmetric-tree | 1ms recursive Java Solution, easy to understand | 1ms-recursive-java-solution-easy-to-unde-68nj | public boolean isSymmetric(TreeNode root) {\n if(root==null) return true;\n return isMirror(root.left,root.right);\n }\n public boolean isM | yangneu2015 | NORMAL | 2015-11-01T18:20:28+00:00 | 2018-10-17T21:13:16.046628+00:00 | 41,060 | false | public boolean isSymmetric(TreeNode root) {\n if(root==null) return true;\n return isMirror(root.left,root.right);\n }\n public boolean isMirror(TreeNode p, TreeNode q) {\n if(p==null && q==null) return true;\n if(p==null || q==null) return false;\n return (p.val==q.val) &&... | 270 | 3 | [] | 31 |
symmetric-tree | My C++ Accepted code in 16ms with iteration solution | my-c-accepted-code-in-16ms-with-iteratio-o7ad | /**\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeN | jayfonlin | NORMAL | 2014-10-23T06:13:59+00:00 | 2018-09-04T23:38:33.406717+00:00 | 51,207 | false | /**\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n class Solution {\n public:\n bool isSymmetric(TreeNode *root) {\n ... | 237 | 3 | [] | 25 |
symmetric-tree | 15 lines of c++ solution / 8 ms | 15-lines-of-c-solution-8-ms-by-pankit-zwwm | bool isSymmetric(TreeNode *root) {\n if (!root) return true;\n return helper(root->left, root->right);\n }\n \n bool | pankit | NORMAL | 2015-03-01T18:00:27+00:00 | 2015-03-01T18:00:27+00:00 | 28,323 | false | bool isSymmetric(TreeNode *root) {\n if (!root) return true;\n return helper(root->left, root->right);\n }\n \n bool helper(TreeNode* p, TreeNode* q) {\n if (!p && !q) {\n return true;\n } else if (!p || !q) {\n return fa... | 210 | 1 | ['Recursion'] | 26 |
symmetric-tree | C++ easy solution with comments 0ms 100% faster | c-easy-solution-with-comments-0ms-100-fa-iwd9 | \nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n \n if(root==NULL) return true; //Tree is empty\n \n return isS | Akshatkamboj37 | NORMAL | 2021-02-14T05:07:51.033207+00:00 | 2021-11-09T08:27:59.834373+00:00 | 20,572 | false | ```\nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n \n if(root==NULL) return true; //Tree is empty\n \n return isSymmetricTest(root->left,root->right);\n }\n \n bool isSymmetricTest(TreeNode* p , TreeNode* q){\n if(p == NULL && q == NULL) //left & right n... | 208 | 1 | ['C', 'C++'] | 21 |
symmetric-tree | 【Video】Going to left side and right side at the same time | video-going-to-left-side-and-right-side-vxt9k | Intuition\nGoing to left side and right side at the same time\n\n# Solution Video\n\nhttps://youtu.be/ywAZyIjRmoo\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget | niits | NORMAL | 2024-12-02T14:19:12.798940+00:00 | 2024-12-02T14:19:31.974377+00:00 | 17,602 | false | # Intuition\nGoing to left side and right side at the same time\n\n# Solution Video\n\nhttps://youtu.be/ywAZyIjRmoo\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n... | 168 | 0 | ['Tree', 'Depth-First Search', 'Binary Tree', 'C++', 'Java', 'Python3', 'JavaScript'] | 1 |
symmetric-tree | Easiest Beginner Friendly Sol with Diagram || DFS || O(n) time and O(h) space | easiest-beginner-friendly-sol-with-diagr-45ql | For Iterative approach please find below link :\nhttps://leetcode.com/problems/symmetric-tree/solutions/3290155/day-72-with-diagram-iterative-and-recursive-easi | singhabhinash | NORMAL | 2023-01-24T13:27:11.851093+00:00 | 2023-04-01T10:52:43.234100+00:00 | 18,766 | false | **For Iterative approach please find below link :**\nhttps://leetcode.com/problems/symmetric-tree/solutions/3290155/day-72-with-diagram-iterative-and-recursive-easiest-beginner-friendly-sol/\n# Intuition\nWe need to validate only 3 conditions including base condition and recursively call to the function:\n- If both "le... | 150 | 0 | ['Depth-First Search', 'Binary Tree', 'Python', 'C++', 'Java'] | 7 |
symmetric-tree | 6line AC python | 6line-ac-python-by-so_kid-n030 | \n\n\n def isSymmetric(self, root):\n def isSym(L,R):\n if not L and not R: return True\n if L and R and L.val = | so_kid | NORMAL | 2015-02-05T11:08:19+00:00 | 2018-10-02T22:13:09.092019+00:00 | 25,735 | false | \n\n\n def isSymmetric(self, root):\n def isSym(L,R):\n if not L and not R: return True\n if L and R and L.val == R.val: \n return isSym(L.left, R.right) and isSym(L.right, R.left)\n return False\n return isSym(root, root) | 149 | 2 | ['Python'] | 13 |
symmetric-tree | Easy || 0 ms 100% (Fully Explained)(Java, C++, Python, JS, Python3) | easy-0-ms-100-fully-explainedjava-c-pyth-kk8m | Java Solution:\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Symmetric Tree.\n\nclass Solution {\n public boolean isSymmetric(TreeNode r | PratikSen07 | NORMAL | 2022-08-14T09:26:26.634288+00:00 | 2022-08-14T09:30:00.630963+00:00 | 29,406 | false | # **Java Solution:**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Symmetric Tree.\n```\nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n // Soecial case...\n if (root == null)\n\t\t return true;\n // call the function recursively...\n\t return isSymme... | 148 | 0 | ['Recursion', 'C', 'Python', 'Java', 'Python3', 'JavaScript'] | 9 |
symmetric-tree | Python short recursive and iterative solutions | python-short-recursive-and-iterative-sol-9vb5 | \n def isSymmetric(self, root):\n if not root:\n return True\n return self.dfs(root.left, root.right)\n \n def dfs(self, l | oldcodingfarmer | NORMAL | 2015-08-13T08:20:18+00:00 | 2020-09-06T14:32:07.843624+00:00 | 17,948 | false | \n def isSymmetric(self, root):\n if not root:\n return True\n return self.dfs(root.left, root.right)\n \n def dfs(self, l, r):\n if l and r:\n return l.val == r.val and self.dfs(l.left, r.right) and self.dfs(l.right, r.left)\n return l == r\n\t\t\n\tdef is... | 136 | 1 | ['Recursion', 'Python'] | 11 |
symmetric-tree | Short and clean java iterative solution | short-and-clean-java-iterative-solution-m0h8k | public boolean isSymmetric(TreeNode root) {\n Queue<TreeNode> q = new LinkedList<TreeNode>();\n if(root == null) return true;\n | moorelee10 | NORMAL | 2015-06-23T18:22:46+00:00 | 2018-10-26T22:30:21.017303+00:00 | 14,670 | false | public boolean isSymmetric(TreeNode root) {\n Queue<TreeNode> q = new LinkedList<TreeNode>();\n if(root == null) return true;\n q.add(root.left);\n q.add(root.right);\n while(q.size() > 1){\n TreeNode left = q.poll(),\n r... | 126 | 0 | [] | 17 |
symmetric-tree | JavaScript recursive and iterative solutions | javascript-recursive-and-iterative-solut-ir5x | The idea is to check whether the tree's left and right subtrees are mirroring each other, we can use preorder traversal:\n\nvar isSymmetric = function(root) {\n | jeantimex | NORMAL | 2017-10-12T21:42:50.222000+00:00 | 2018-09-15T07:22:47.524840+00:00 | 6,102 | false | The idea is to check whether the tree's left and right subtrees are mirroring each other, we can use preorder traversal:\n```\nvar isSymmetric = function(root) {\n if (!root) { // Sanity check\n return true;\n }\n\n // Check if tree s & t are mirroring each other\n function isMirror(s, t) {\n ... | 116 | 1 | [] | 11 |
symmetric-tree | Image Explanation🏆 - [Recursive & Non-Recursive] - Complete Intuition | image-explanation-recursive-non-recursiv-fzzy | Video Solution\nhttps://youtu.be/41j4iR4Gx9s\n\n# Approach & Intuition\n\n\n\n\n\n\n\n\n\n\n\n\n# Recursive Code\n\nclass Solution {\npublic:\n bool isSymmet | aryan_0077 | NORMAL | 2023-03-13T01:12:03.110553+00:00 | 2023-03-13T02:14:23.682467+00:00 | 11,628 | false | # Video Solution\nhttps://youtu.be/41j4iR4Gx9s\n\n# Approach & Intuition\n\n\n + BFS (Queue) Solution in Java | ismirror-dfs-recursion-onetwo-stacks-bfs-i4ld | Reference: LeetCode EPI 9.2\nDifficulty: Easy\n\n## Problem\n\n> Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).\ | junhaowanggg | NORMAL | 2019-11-19T18:28:34.146230+00:00 | 2019-11-19T18:28:34.146276+00:00 | 5,117 | false | Reference: [LeetCode](https://leetcode.com/problems/symmetric-tree/) <span class="gray">EPI 9.2</span>\nDifficulty: <span class="green">Easy</span>\n\n## Problem\n\n> Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).\n\n**Example:** \n\n`[1,2,2,3,4,4,3]` is symmetric:\n\n```... | 84 | 0 | ['Breadth-First Search', 'Recursion', 'Iterator', 'Java'] | 5 |
symmetric-tree | Recursive and iterative (DFS and BFS) in C++. Easy to understand. | recursive-and-iterative-dfs-and-bfs-in-c-teg7 | Iterative in BFS:\n\n bool isSymmetric(TreeNode root) {\n if(!root) return true;\n queue q;\n q.push(make_pair(root->left, root->right)) | 1grc1t86 | NORMAL | 2016-02-24T08:00:36+00:00 | 2016-02-24T08:00:36+00:00 | 9,170 | false | **Iterative in BFS**:\n\n bool isSymmetric(TreeNode* root) {\n if(!root) return true;\n queue<nodepair> q;\n q.push(make_pair(root->left, root->right));\n while(!q.empty()){\n nodepair p = q.front();\n q.pop();\n if(!p.first && !p.second) continue;\n ... | 63 | 0 | ['Depth-First Search', 'Breadth-First Search', 'C++'] | 9 |
symmetric-tree | ✅✅C++ || Easy Solution || 💯💯Recursive Approach || Heavily Commented | c-easy-solution-recursive-approach-heavi-33qg | \u2705\u2705C++ || Easy Solution || \uD83D\uDCAF\uD83D\uDCAFRecursive Approach || Heavily Commented\n# Please Upvote as it really motivates me\n\n\nclass Soluti | Conquistador17 | NORMAL | 2023-03-13T02:17:48.190057+00:00 | 2023-03-13T02:17:48.190087+00:00 | 6,331 | false | ## **\u2705\u2705C++ || Easy Solution || \uD83D\uDCAF\uD83D\uDCAFRecursive Approach || Heavily Commented**\n# **Please Upvote as it really motivates me**\n\n```\nclass Solution {\npublic:\n bool isEqual(TreeNode*r1,TreeNode*r2){\n //if we have both root to nullptr then we will return true\n //else we w... | 55 | 0 | ['Recursion', 'C', 'C++'] | 3 |
symmetric-tree | C++ | Recursive | Well Commented | c-recursive-well-commented-by-abhi_vee-a14o | \nclass Solution {\npublic:\n bool solve(TreeNode * r1, TreeNode * r2)\n { \n if(r1 == NULL && r2 == NULL)\n return true; \n\t\t\n | abhi_vee | NORMAL | 2021-08-12T07:32:58.738468+00:00 | 2022-04-03T10:18:00.454469+00:00 | 3,628 | false | ```\nclass Solution {\npublic:\n bool solve(TreeNode * r1, TreeNode * r2)\n { \n if(r1 == NULL && r2 == NULL)\n return true; \n\t\t\n else if(r1 == NULL || r2 == NULL || r1->val != r2->val)\n return false; \n \n return solve(r1->left, r2->right) && solve(r1->ri... | 47 | 0 | ['Recursion', 'C', 'C++'] | 3 |
symmetric-tree | Python iterative way using a queue | python-iterative-way-using-a-queue-by-sh-es7p | Each iteration, it checks whether two nodes are symmetric and then push (node1.left, node2.right), (node1.right, node2.left) to the end of queue.\n\n class S | shawny | NORMAL | 2015-02-26T10:56:43+00:00 | 2018-09-21T19:38:42.277582+00:00 | 7,608 | false | Each iteration, it checks whether two nodes are symmetric and then push (node1.left, node2.right), (node1.right, node2.left) to the end of queue.\n\n class Solution:\n # @param root, a tree node\n # @return a boolean\n def isSymmetric(self, root):\n if not root:\n return True\n\n dq... | 43 | 0 | ['Python'] | 4 |
symmetric-tree | [Javascript] 95% speed, 100% memory - w/ comments | javascript-95-speed-100-memory-w-comment-m5lm | \nvar isSymmetric = function(root) {\n if (root == null) return true;\n \n return symmetryChecker(root.left, root.right);\n};\n\nfunction symmetryCheck | user9682w | NORMAL | 2020-01-31T02:31:42.074272+00:00 | 2020-02-02T15:45:35.248408+00:00 | 4,832 | false | ```\nvar isSymmetric = function(root) {\n if (root == null) return true;\n \n return symmetryChecker(root.left, root.right);\n};\n\nfunction symmetryChecker(left, right) {\n if (left == null && right == null) return true; // If both sub trees are empty\n if (left == null || right == null) return false; /... | 41 | 0 | ['JavaScript'] | 2 |
symmetric-tree | [ Python ] ✅✅ Simple Python Solution Using Recursion 🥳✌👍 | python-simple-python-solution-using-recu-gfp2 | If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 41 ms, faster than 29.46% of Python3 online submission | ashok_kumar_meghvanshi | NORMAL | 2022-02-28T03:41:43.569372+00:00 | 2023-03-13T08:25:01.682540+00:00 | 3,879 | false | # If You like the Solution, Don\'t Forget To UpVote Me, Please UpVote! \uD83D\uDD3C\uD83D\uDE4F\n# Runtime: 41 ms, faster than 29.46% of Python3 online submissions for Symmetric Tree.\n# Memory Usage: 13.8 MB, less than 99.68% of Python3 online submissions for Symmetric Tree.\n\tclass Solution:\n\t\tdef isSymmetric(sel... | 36 | 0 | ['Recursion', 'Python', 'Python3'] | 5 |
symmetric-tree | Slim Java solution | slim-java-solution-by-alexthegreat-ecsc | The idea is:\n1. level traversal.\n2. push nodes onto stack, every 2 consecutive is a pair, and should either be both null or have equal value.\nrepeat until st | alexthegreat | NORMAL | 2014-12-30T02:42:24+00:00 | 2014-12-30T02:42:24+00:00 | 5,408 | false | The idea is:\n1. level traversal.\n2. push nodes onto stack, every 2 consecutive is a pair, and should either be both null or have equal value.\nrepeat until stack is empty.\n\n public boolean isSymmetric(TreeNode root) {\n if (root == null)\n return true;\n Stack<TreeNode> stack = new Stack... | 33 | 0 | [] | 6 |
symmetric-tree | Another passed Java solution | another-passed-java-solution-by-jeantime-cndg | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n if (root == null) \n return true;\n \n | jeantimex | NORMAL | 2015-06-28T17:40:22+00:00 | 2015-06-28T17:40:22+00:00 | 3,097 | false | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n if (root == null) \n return true;\n \n return isSymmetric(root.left, root.right);\n }\n \n boolean isSymmetric(TreeNode left, TreeNode right) {\n if (left ==... | 28 | 0 | ['Java'] | 2 |
symmetric-tree | python, best and explained O(n) recursion | python-best-and-explained-on-recursion-b-vpk7 | python\nclass Solution:\n def isSymmetric(self, root: TreeNode) -> bool:\n if not root:\n return True\n \n def isMirror(tree1 | serdarkuyuk | NORMAL | 2020-11-18T05:26:46.325291+00:00 | 2020-11-18T05:26:46.325334+00:00 | 3,728 | false | ```python\nclass Solution:\n def isSymmetric(self, root: TreeNode) -> bool:\n if not root:\n return True\n \n def isMirror(tree1, tree2):\n if not tree1 or not tree2: # if one of them is null\n return tree2 == tree1 # compare them\n if tree1.val !... | 27 | 1 | ['Recursion', 'Python', 'Python3'] | 2 |
symmetric-tree | [Python 3] DFS iterative solution using stack. Easy to understand. | python-3-dfs-iterative-solution-using-st-byz1 | \nclass Solution:\n def isSymmetric(self, root):\n stack = []\n if root: stack.append([root.left, root.right])\n\n while(len(stack) > 0) | abstractart | NORMAL | 2020-07-21T21:02:14.618256+00:00 | 2020-08-21T07:57:08.760565+00:00 | 2,101 | false | ```\nclass Solution:\n def isSymmetric(self, root):\n stack = []\n if root: stack.append([root.left, root.right])\n\n while(len(stack) > 0):\n left, right = stack.pop()\n \n if left and right:\n if left.val != right.val: return False\n ... | 27 | 0 | ['Python', 'Python3'] | 5 |
symmetric-tree | Share my recursive C++ solution,easy to understand | share-my-recursive-c-solutioneasy-to-und-eimm | class Solution {\n public:\n bool isSymmetric(TreeNode* root) {\n if (root == NULL)\n return true;\n \n | vdvvdd | NORMAL | 2016-01-10T06:38:25+00:00 | 2016-01-10T06:38:25+00:00 | 2,188 | false | class Solution {\n public:\n bool isSymmetric(TreeNode* root) {\n if (root == NULL)\n return true;\n \n return checkSymmetric(root->left, root->right);\n }\n //check the two nodes in symmetric position\n bool checkSymmetric(TreeNode *lef... | 27 | 1 | ['C++'] | 3 |
symmetric-tree | ✅🚀 TS Easy Solution || Recursive | ts-easy-solution-recursive-by-viniciuste-gigw | Let me know in comments if you have any doubts. I will be happy to answer.\nPlease upvote if you found the solution useful.\n\n\nconst isSymmetric = (root: Tree | viniciusteixeiradias | NORMAL | 2022-11-02T22:50:20.104722+00:00 | 2022-11-02T22:50:39.978319+00:00 | 2,565 | false | Let me know in comments if you have any doubts. I will be happy to answer.\nPlease upvote if you found the solution useful.\n\n```\nconst isSymmetric = (root: TreeNode | null): boolean => {\n return isMirror(root, root);\n};\n \nconst isMirror = (t1: TreeNode | null, t2: TreeNode | null): boolean => {\n if (t1 === n... | 26 | 0 | ['TypeScript', 'JavaScript'] | 3 |
symmetric-tree | Easy and simple using one queue iterative in java | easy-and-simple-using-one-queue-iterativ-8lln | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n if(root == null) return true;\n Queue<TreeNode> queue = new | chen2 | NORMAL | 2015-11-21T05:42:31+00:00 | 2018-08-30T03:03:54.649078+00:00 | 2,519 | false | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n if(root == null) return true;\n Queue<TreeNode> queue = new LinkedList<TreeNode>();\n queue.offer(root.left);\n queue.offer(root.right);\n while(!queue.isEmpty()){\n T... | 26 | 0 | [] | 5 |
symmetric-tree | ✅ [PHP][JavaScript] Recursive & Iterative Solutions | phpjavascript-recursive-iterative-soluti-j1h9 | 1. Recursive Solution\nThis solution uses recursion to check if a binary tree is symmetric. The recursive helper function isMirror() checks if the two nodes of | akunopaka | NORMAL | 2023-03-13T18:25:04.624745+00:00 | 2023-03-13T18:28:49.286169+00:00 | 1,849 | false | ### 1. Recursive Solution\nThis solution uses recursion to check if a binary tree is symmetric. The recursive helper function isMirror() checks if the two nodes of the binary tree are mirror images of each other, by comparing their values, and then recursively checking if their left and right nodes are mirror images of... | 25 | 0 | ['PHP', 'JavaScript'] | 3 |
symmetric-tree | Easy to understand Python | easy-to-understand-python-by-cglotr-0kag | Recursive\n\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# | cglotr | NORMAL | 2019-06-14T14:35:06.863731+00:00 | 2019-06-14T14:44:14.846920+00:00 | 2,814 | false | Recursive\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def isSymmetric(self, root: TreeNode) -> bool:\n if not root:\n return True\n return sel... | 25 | 0 | ['Stack', 'Depth-First Search', 'Recursion', 'Iterator', 'Python'] | 2 |
symmetric-tree | Elegant Swift solution by conforming to Equatable | elegant-swift-solution-by-conforming-to-c38ae | \nclass Solution {\n func isSymmetric(_ root: TreeNode?) -> Bool {\n\t\troot?.left == root?.right\n\t}\n}\n\nextension TreeNode: Equatable {\n public stat | hak0suka | NORMAL | 2020-10-04T15:50:05.264510+00:00 | 2020-11-10T11:04:58.170827+00:00 | 808 | false | ```\nclass Solution {\n func isSymmetric(_ root: TreeNode?) -> Bool {\n\t\troot?.left == root?.right\n\t}\n}\n\nextension TreeNode: Equatable {\n public static func ==(lhs: TreeNode, rhs: TreeNode) -> Bool {\n lhs.val == rhs.val && lhs.left == rhs.right && lhs.right == rhs.left\n }\n}\n``` | 24 | 0 | ['Swift'] | 4 |
symmetric-tree | Python solution | python-solution-by-zitaowang-6asi | Recursive:\n\nclass Solution(object):\n def isSymmetric(self, root):\n """\n :type root: TreeNode\n :rtype: bool\n """\n d | zitaowang | NORMAL | 2018-08-17T01:46:04.211018+00:00 | 2018-08-17T01:46:04.211063+00:00 | 2,332 | false | Recursive:\n```\nclass Solution(object):\n def isSymmetric(self, root):\n """\n :type root: TreeNode\n :rtype: bool\n """\n def isSym(root1, root2):\n if root1 == None and root2 == None:\n return True\n elif root1 == None and root2 != None:\n ... | 23 | 0 | [] | 3 |
symmetric-tree | Explained with images 💻. Easy to understand. 📝 | explained-with-images-easy-to-understand-3yih | Approach\nStarting from the root, we have to check that the left and right subtrees of the tree are mirrors of each other. The tree containing a single element | Kunal_Tajne | NORMAL | 2024-08-18T15:44:47.805736+00:00 | 2024-08-18T15:50:31.619994+00:00 | 2,148 | false | # Approach\nStarting from the root, we have to check that the left and right subtrees of the tree are mirrors of each other. The tree containing a single element root is always symmetric; we will return TRUE in this case. Otherwise, we will use the following algorithm:\n\n- We take a queue with root\'s right and left n... | 22 | 0 | ['Tree', 'Breadth-First Search', 'Binary Tree', 'C++', 'Java'] | 2 |
symmetric-tree | ✅ 🔥 0 ms Runtime Beats 100% User🔥|| Code Idea ✅ || Algorithm & Solving Step ✅ || | 0-ms-runtime-beats-100-user-code-idea-al-s33p | \n\n\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n\n### Key Idea :\nA binary tree is symmetric if:\n1. Its left and right subtrees hav | Letssoumen | NORMAL | 2024-11-28T03:09:23.360619+00:00 | 2024-11-28T03:09:23.360660+00:00 | 3,365 | false | \n\n\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n\n### **Key Idea** :\nA binary tree is symmetric if:\n1. Its left and right subtrees have the s... | 20 | 0 | ['Tree', 'Depth-First Search', 'C++', 'Java', 'Python3'] | 1 |
symmetric-tree | JAVA | BFS and DFS ✅ | java-bfs-and-dfs-by-sourin_bruh-gs6d | Please Upvote \uD83D\uDE07\n---\n##### 1. DFS approach:\n\nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n return help(root.left, roo | sourin_bruh | NORMAL | 2022-10-27T18:17:35.190824+00:00 | 2023-03-13T08:59:19.550370+00:00 | 2,336 | false | # Please Upvote \uD83D\uDE07\n---\n##### 1. DFS approach:\n```\nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n return help(root.left, root.right);\n }\n\n private boolean help(TreeNode left, TreeNode right) {\n if (left == null || right == null) {\n return left == rig... | 18 | 0 | ['Tree', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Java'] | 2 |
symmetric-tree | JAVA ||Easy Recursive solution || Easiest Approach || Runtime: 0 ms | java-easy-recursive-solution-easiest-app-xsgx | \nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n return root == null || isMirror(root.left, root.right);\n }\n boolean isMirror | duke05 | NORMAL | 2022-10-11T07:52:08.023926+00:00 | 2022-10-11T07:52:08.023958+00:00 | 2,440 | false | ```\nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n return root == null || isMirror(root.left, root.right);\n }\n boolean isMirror(TreeNode node1, TreeNode node2) {\n if (node1 == null && node2 == null) return true;\n \n if (node1 == null || node2 == null) return fa... | 18 | 0 | ['Depth-First Search', 'Binary Tree', 'Java'] | 2 |
symmetric-tree | Python Solution with EASY Explanation | python-solution-with-easy-explanation-by-fhab | Please Upvote if it helps ! \n\n---\n\n- # Intuition\n Describe your first thoughts on how to solve this problem. \n\nTo check whether a tree is mirror symmetri | namanpuri | NORMAL | 2023-03-13T05:50:58.177826+00:00 | 2023-03-13T05:50:58.177866+00:00 | 3,118 | false | > # *Please Upvote if it helps !* \n\n---\n\n- # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nTo check whether a tree is mirror symmetric or not, we need to compare the left and right subtrees of each node. If they are mirror images of each other, then the tree is mirror symmetric.\... | 17 | 0 | ['Python3'] | 3 |
symmetric-tree | Python | DFS | BFS | python-dfs-bfs-by-kevinjm17-l2le | DFS Solution\n\nclass Solution:\n def isSymmetric(self, root: Optional[TreeNode]) -> bool:\n if not root:\n return True\n \n | kevinjm17 | NORMAL | 2022-09-28T19:56:43.325680+00:00 | 2022-09-28T19:57:52.762364+00:00 | 1,930 | false | **DFS Solution**\n```\nclass Solution:\n def isSymmetric(self, root: Optional[TreeNode]) -> bool:\n if not root:\n return True\n \n def dfs(l, r):\n if not l and not r:\n return True\n \n if not l or not r:\n return False\... | 17 | 0 | ['Depth-First Search', 'Breadth-First Search', 'Python'] | 0 |
symmetric-tree | 2 lines Java solution use 1ms | 2-lines-java-solution-use-1ms-by-zbl-tqr6 | \n\n public class Solution {\n public boolean isSymmetric(TreeNode root) {\n return isMirror(root,root);\n }\n \n public b | zbl | NORMAL | 2016-04-26T09:10:59+00:00 | 2016-04-26T09:10:59+00:00 | 1,909 | false | \n\n public class Solution {\n public boolean isSymmetric(TreeNode root) {\n return isMirror(root,root);\n }\n \n public boolean isMirror(TreeNode a,TreeNode b){\n return a==null||b==null?a==b:a.val==b.val&&isMirror(a.left,b.right)&&isMirror(a.right,b.left);\n }\n... | 17 | 0 | [] | 2 |
symmetric-tree | Beautiful Recursive and Iterative Solutions | beautiful-recursive-and-iterative-soluti-d1sp | Very simple ideas. Notice how both look similar to each other.\n\n /\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n | 1337beef | NORMAL | 2014-09-18T15:03:54+00:00 | 2014-09-18T15:03:54+00:00 | 6,663 | false | Very simple ideas. Notice how both look similar to each other.\n\n /**\n * Definition for binary tree\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n #include<queue>\n... | 16 | 3 | [] | 10 |
symmetric-tree | java || easiest solution | java-easiest-solution-by-ashutosh_369-29lq | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Ashutosh_369 | NORMAL | 2023-03-13T02:17:37.231464+00:00 | 2023-03-13T02:17:37.231507+00:00 | 2,334 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 15 | 0 | ['Java'] | 0 |
symmetric-tree | Day 72 || With Diagram || Iterative and Recursive || Easiest Beginner Friendly Sol | day-72-with-diagram-iterative-and-recurs-7tss | NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.\n\n# Intuitio | singhabhinash | NORMAL | 2023-03-13T00:50:21.879829+00:00 | 2023-03-13T05:14:40.613943+00:00 | 1,479 | false | **NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem:\nWe need to validate only 3 conditions including base condition and recursively call to the function:\n- If both "leftRoot" and "rightRoo... | 15 | 5 | ['Depth-First Search', 'Binary Tree', 'C++', 'Java', 'Python3'] | 1 |
symmetric-tree | C++ short simple recursive code | c-short-simple-recursive-code-by-jeetaks-n2zz | \n# Approach\n Describe your approach to solving the problem. \nRecurively check for the subtrees. Return false if they aren\'t equal.\n\n# Code\n\nclass Soluti | Jeetaksh | NORMAL | 2023-01-15T07:52:48.191088+00:00 | 2023-01-15T07:52:48.191119+00:00 | 2,377 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nRecurively check for the subtrees. Return false if they aren\'t equal.\n\n# Code\n```\nclass Solution {\npublic:\n\n bool isSame(TreeNode* t1, TreeNode* t2){\n if(!t1 && !t2){return true;}\n if(t1 && !t2){return false;}\n if(... | 15 | 0 | ['C++'] | 0 |
symmetric-tree | ✔️ 100% Fastest Swift Solution, time: O(n), space: O(n). | 100-fastest-swift-solution-time-on-space-r0w7 | \n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right | sergeyleschev | NORMAL | 2022-04-08T13:41:27.305857+00:00 | 2022-04-08T13:41:27.305903+00:00 | 1,299 | false | ```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right: TreeNode?\n * public init() { self.val = 0; self.left = nil; self.right = nil; }\n * public init(_ val: Int) { self.val = val; self.left = nil; sel... | 15 | 0 | ['Swift'] | 0 |
symmetric-tree | 100% faster 0ms runtime short C++ solution | 100-faster-0ms-runtime-short-c-solution-36njz | \n\n\n bool isMirror(TreeNode* root1,TreeNode *root2){\n if(root1==NULL && root2==NULL)\n return true;\n if(root1 && root2 && root1- | reetisharma | NORMAL | 2021-03-21T15:54:08.295678+00:00 | 2021-03-21T15:54:08.295722+00:00 | 1,328 | false | ```\n\n\n bool isMirror(TreeNode* root1,TreeNode *root2){\n if(root1==NULL && root2==NULL)\n return true;\n if(root1 && root2 && root1->val == root2->val)\n return isMirror(root1->left,root2->right) && isMirror(root1->right,root2->left);\n \n return false;\n }\n ... | 15 | 1 | ['Recursion', 'C'] | 1 |
symmetric-tree | c++ solution recursive and iterative | c-solution-recursive-and-iterative-by-fi-5l9o | Note\nThe answer is true when root is nullptr.\n\nclass Solution\n{\npublic:\n\t// recursive : 12 ms 16.7 MB\n\tbool isSymmetric(TreeNode* root) {\n\t\treturn ! | fifamvp | NORMAL | 2020-11-22T08:49:20.436906+00:00 | 2020-11-22T08:49:20.436948+00:00 | 1,302 | false | **Note**\nThe answer is true when root is nullptr.\n```\nclass Solution\n{\npublic:\n\t// recursive : 12 ms 16.7 MB\n\tbool isSymmetric(TreeNode* root) {\n\t\treturn !root || isEquivalent(root->left, root->right);\n\t}\n\n\tbool isEquivalent(TreeNode* leftNode, TreeNode* rightNode)\n\t{\n\t\tif (!leftNode && rightNode ... | 15 | 0 | ['Recursion', 'C', 'Iterator'] | 1 |
symmetric-tree | Clean iterative solution in C++ | clean-iterative-solution-in-c-by-bei-zi-aosvz | /**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * | bei-zi-jun | NORMAL | 2015-05-26T18:03:51+00:00 | 2015-05-26T18:03:51+00:00 | 2,232 | false | /**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n class Solution {\n public:\n bool isSymmetric(TreeNode* root) {\n... | 15 | 2 | [] | 1 |
symmetric-tree | 4ms Simple C++ code | 4ms-simple-c-code-by-davidyw-a9av | class Solution {\n public:\n bool isSymmetric(TreeNode* root) {\n if (!root){\n return true;\n }\n else{\n | davidyw | NORMAL | 2015-09-27T22:12:04+00:00 | 2015-09-27T22:12:04+00:00 | 2,199 | false | class Solution {\n public:\n bool isSymmetric(TreeNode* root) {\n if (!root){\n return true;\n }\n else{\n return isSame(root->left, root->right);\n }\n }\n private: // hide functions in the private helps to improve running ti... | 15 | 0 | ['Recursion', 'C++'] | 3 |
symmetric-tree | *Java* iterative & recursive solutions | java-iterative-recursive-solutions-by-el-mnj7 | Recursive#\n public boolean isSymmetric(TreeNode root) {\n \tif(root==null) return true;\n \treturn isSymmetric(root.left, root.right);\n }\n pri | elementnotfoundexception | NORMAL | 2016-01-23T06:37:03+00:00 | 2016-01-23T06:37:03+00:00 | 2,460 | false | #Recursive#\n public boolean isSymmetric(TreeNode root) {\n \tif(root==null) return true;\n \treturn isSymmetric(root.left, root.right);\n }\n private boolean isSymmetric(TreeNode root1, TreeNode root2) {\n \tif(root1==null && root2==null) return true;\n \tif(root1==null || root2==null) return fals... | 15 | 0 | ['Java'] | 1 |
symmetric-tree | ✅ C++ solution | Easy to understand | O(N) | c-solution-easy-to-understand-on-by-kosa-v0jt | Complexity\n- Time complexity: O(1)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co | kosant | NORMAL | 2023-05-13T06:34:27.763208+00:00 | 2023-05-13T06:34:27.763247+00:00 | 2,209 | false | # Complexity\n- Time complexity: O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool isEqual(TreeNode* left, TreeNode* right) {\n if (!left && !right)\n retur... | 14 | 0 | ['Tree', 'C', 'Binary Tree', 'C++'] | 1 |
symmetric-tree | 0 ms100% Iteration && Recursion | 0-ms100-iteration-recursion-by-shikuan-7vb0 | Iteration\n\ngolang\nfunc isSymmetric(root *TreeNode) bool {\n if root == nil {\n\t\treturn true\n\t}\n\tvar stack []*TreeNode\n\tstack = append(stack, root. | shikuan | NORMAL | 2022-11-09T15:12:08.136681+00:00 | 2022-11-09T15:12:08.136724+00:00 | 1,772 | false | #### Iteration\n\n```golang\nfunc isSymmetric(root *TreeNode) bool {\n if root == nil {\n\t\treturn true\n\t}\n\tvar stack []*TreeNode\n\tstack = append(stack, root.Left, root.Right)\n\tfor len(stack) > 0 {\n\t\tl, r := stack[0], stack[1]\n\t\tstack = stack[2:]\n\t\tif l == nil && r == nil {\n\t\t\tcontinue\n\t\t} e... | 14 | 0 | ['Go'] | 2 |
symmetric-tree | Facebook, Amazon Interview, 100ms Easy to understand, Very clean code, TC: O(N), SC: O(N) | facebook-amazon-interview-100ms-easy-to-1vugd | \nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n \n if(root==NULL) return true; //To check if tree is empty or not\n \ | DarshanAgarwal95 | NORMAL | 2022-05-30T03:26:52.315163+00:00 | 2022-05-30T03:27:29.330714+00:00 | 637 | false | ```\nclass Solution {\npublic:\n bool isSymmetric(TreeNode* root) {\n \n if(root==NULL) return true; //To check if tree is empty or not\n \n return isSymmetricTest(root->left,root->right);\n }\n bool isSymmetricTest(TreeNode* p , TreeNode* q){\n if(p == NULL && q == NULL) // ... | 14 | 0 | ['Depth-First Search', 'C', 'Binary Tree'] | 0 |
symmetric-tree | Py3 Sol: in just few lines, 24ms, beats 97.63% | py3-sol-in-just-few-lines-24ms-beats-976-ceob | \ndef isSymmetricBst(node1, node2):\n if node1==None and node2==None:\n return True\n elif node1==None or node2==None:\n return False\n e | ycverma005 | NORMAL | 2020-03-10T07:47:55.812098+00:00 | 2020-03-10T07:53:54.629140+00:00 | 2,901 | false | ```\ndef isSymmetricBst(node1, node2):\n if node1==None and node2==None:\n return True\n elif node1==None or node2==None:\n return False\n else:\n return node1.val==node2.val and isSymmetricBst(node1.left,node2.right) and isSymmetricBst(node1.right, node2.left)\n \nclass Solution:\n ... | 14 | 0 | ['Python', 'Python3'] | 4 |
symmetric-tree | ☕Java Solution | 🚀0ms Runtime - beats 100%🔥| Easy to Understand😇 | java-solution-0ms-runtime-beats-100-easy-420a | Intuition\n Describe your first thoughts on how to solve this problem. \nBy seeing the problem my first intuition was to use BFS to solve the problem,by taking | 5p7Ro0t | NORMAL | 2023-02-25T07:05:28.582803+00:00 | 2023-02-25T08:02:32.255267+00:00 | 1,906 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBy seeing the problem my first intuition was to use BFS to solve the problem,by taking a queue but as i approached the problem i understand that using DFS is more optimal and less complex to solve this problem.\n# Approach\n<!-- Describe ... | 13 | 0 | ['Tree', 'Depth-First Search', 'Recursion', 'Java'] | 3 |
symmetric-tree | [Python] Simple BFS Iterative Approach with Explaination | python-simple-bfs-iterative-approach-wit-7jf1 | Think in pairs would really help. \nTo check if a tree is symmetric, we need a BFS on 2 sides of symmetry: left and right. If left and right value match, we sho | judyzzy | NORMAL | 2020-05-21T01:22:33.406659+00:00 | 2020-05-21T01:22:33.406781+00:00 | 851 | false | Think in pairs would really help. \nTo check if a tree is symmetric, we need a BFS on 2 sides of symmetry: left and right. If left and right value match, we should proceed. \nThe key is to add the children of left and right in the proper order: outter match, then inner match. Because we are queuing in pairs, the childr... | 13 | 0 | ['Breadth-First Search', 'Python'] | 0 |
symmetric-tree | Symmetric Tree Solution in C++ | symmetric-tree-solution-in-c-by-the_kuna-wfqd | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | The_Kunal_Singh | NORMAL | 2023-08-19T14:23:36.401177+00:00 | 2023-08-19T14:23:36.401200+00:00 | 1,190 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 12 | 0 | ['C++'] | 1 |
symmetric-tree | Java | java-by-niyazjava-xvaj | If you like it pls upvote\n\n\n\n public boolean isSymmetric(TreeNode root) {\n if (root == null) return true;\n Stack<TreeNode> stack = new Stack<>();\n sta | NiyazJava | NORMAL | 2022-11-15T14:33:45.719927+00:00 | 2022-11-20T01:35:39.158837+00:00 | 925 | false | If you like it pls upvote\n\n```\n\n public boolean isSymmetric(TreeNode root) {\n if (root == null) return true;\n Stack<TreeNode> stack = new Stack<>();\n stack.push(root.left);\n stack.push(root.right);\n\n while (!stack.isEmpty()) {\n TreeNode n1 = stack.pop();\n TreeNode n2 = stack.pop();\n\n if (n1 == ... | 12 | 0 | ['Java'] | 1 |
symmetric-tree | Recursively and iteratively (level order traversal) in Python | recursively-and-iteratively-level-order-2nxkz | Solution 1. recursively solution\n\n\ndef isSymmetric(self, root):\n\tdef treeMatch(root1, root2):\n\t\tif not root1 and not root2:\n\t\t\treturn True\n\t\tif ( | imssssshan | NORMAL | 2022-01-28T08:34:18.526970+00:00 | 2022-01-28T08:34:18.527016+00:00 | 555 | false | **Solution 1. recursively solution**\n\n```\ndef isSymmetric(self, root):\n\tdef treeMatch(root1, root2):\n\t\tif not root1 and not root2:\n\t\t\treturn True\n\t\tif (root1 and not root2) or (root2 and not root1):\n\t\t\treturn False\n\t\tif root1.val != root2.val:\n\t\t\treturn False\n\t\treturn treeMatch(root1.left, ... | 12 | 0 | ['Binary Tree', 'Iterator', 'Python'] | 1 |
symmetric-tree | Swift: Symmetric Tree (+ Test Cases) | swift-symmetric-tree-test-cases-by-asahi-ww0a | swift\nclass Solution {\n func isSymmetric(_ root: TreeNode?) -> Bool {\n return check(root, root)\n }\n private final func check(_ l: TreeNode? | AsahiOcean | NORMAL | 2021-07-08T05:48:42.017815+00:00 | 2021-07-08T05:48:42.017861+00:00 | 1,107 | false | ```swift\nclass Solution {\n func isSymmetric(_ root: TreeNode?) -> Bool {\n return check(root, root)\n }\n private final func check(_ l: TreeNode?, _ r: TreeNode?) -> Bool {\n if [l,r].allSatisfy({$0 == nil}) { return true }\n if l == nil || r == nil { return false }\n let n = (l?.... | 12 | 0 | ['Swift'] | 0 |
symmetric-tree | Javascript, Iterative, commented | javascript-iterative-commented-by-melgab-4i1w | \n/**\nTime Complexity: O(n)\nSpace Complexity: O(n)\n*/\nvar isSymmetric = function(root) {\n// if there is no root that means it is a symettric tree\n | melgabal | NORMAL | 2020-06-30T04:18:02.662073+00:00 | 2020-06-30T04:18:02.662101+00:00 | 1,504 | false | ```\n/**\nTime Complexity: O(n)\nSpace Complexity: O(n)\n*/\nvar isSymmetric = function(root) {\n// if there is no root that means it is a symettric tree\n if(!root) return true\n// Start 2 queue one for the left banch and one for the right branch\n let q1 = [root.left], q2 = [root.right]\n// traverse... | 12 | 0 | ['Breadth-First Search', 'JavaScript'] | 2 |
symmetric-tree | Concise Recursive Java Solution | concise-recursive-java-solution-by-siyan-ydey | This method is my best recursive try. Use two TreeNode as parameters.\n\nIf you are willing to help, \n\nPlease go to https://oj.leetcode.com/discuss/24968/is-m | siyang3 | NORMAL | 2015-02-10T18:42:35+00:00 | 2015-02-10T18:42:35+00:00 | 1,039 | false | This method is my best recursive try. Use two TreeNode as parameters.\n\nIf you are willing to help, \n\nPlease go to https://oj.leetcode.com/discuss/24968/is-my-method-a-recursive-one-java-solution\nto see my another recursive solution and give some comment on that recursive method.\n \n\n public class Solution {... | 12 | 0 | ['Recursion', 'Java'] | 1 |
symmetric-tree | 0 ms C solution [DoneRecusively] :) | 0-ms-c-solution-donerecusively-by-sai-vjoq | \n\nbool isSymmetricRecursively(struct TreeNode Lnode ,struct TreeNode Rnode ){\n \n if(Lnode==NULL && Rnode==NULL)\n return true;\n \n i | sai | NORMAL | 2015-05-30T16:16:31+00:00 | 2015-05-30T16:16:31+00:00 | 1,497 | false | \n\nbool isSymmetricRecursively(struct TreeNode* Lnode ,struct TreeNode* Rnode ){\n \n if(Lnode==NULL && Rnode==NULL)\n return true;\n \n if(Lnode==NULL || Rnode==NULL)\n return false ;\n \n \n return ((Lnode->val==Rnode->val) &&\n isSymmetricRecursively(Ln... | 12 | 1 | [] | 1 |
symmetric-tree | Java Solution #1ms 4 lines code #Recursive Easy To Understand | java-solution-1ms-4-lines-code-recursive-2kh9 | \tpublic static boolean isSymmetric(TreeNode root) {\n\t\treturn isSymmetric(root,root);\n\t}\n\t\n\tpublic static boolean isSymmetric(TreeNode p, TreeNode q){\ | therealfakebatman | NORMAL | 2016-03-16T21:11:30+00:00 | 2016-03-16T21:11:30+00:00 | 1,286 | false | \tpublic static boolean isSymmetric(TreeNode root) {\n\t\treturn isSymmetric(root,root);\n\t}\n\t\n\tpublic static boolean isSymmetric(TreeNode p, TreeNode q){\n\t\tif(p==null && q==null) return true;\n\t\tif(p==null || q==null) return false;\n\t\t\n\t\treturn p.val ==q.val&&isSymmetric(p.left,q.right)&&isSymmetric(p.r... | 12 | 0 | [] | 2 |
symmetric-tree | [ ITERATIVE (BFS) ] 0 ms beats 100% cpp submissions. | iterative-bfs-0-ms-beats-100-cpp-submiss-rp37 | Intuition\nChecking Tree\'s symmetry comparing values of same level nodes in left and right subtrees.\n\n# Approach\nPerform Breadth-first search using queue da | ScorpioDagger | NORMAL | 2023-03-13T15:59:20.257848+00:00 | 2023-07-24T08:11:53.155791+00:00 | 1,467 | false | # Intuition\nChecking Tree\'s symmetry comparing values of same level nodes in left and right subtrees.\n\n# Approach\nPerform **Breadth-first search** using queue data structure to do a level order traversal of the tree comparing values of corresponding level nodes in left and right subtrees thus know whether symmetri... | 11 | 0 | ['Breadth-First Search', 'Binary Tree', 'C++'] | 0 |
symmetric-tree | 0ms | JAVA | 10 Lines | Recursive | EXPLAINING FULL | FEW LINES | EASY TO UNDERSTAND | 0ms-java-10-lines-recursive-explaining-f-e63z | So the solution is a little simple, \nthe first step is how to build the algorithm if two tree is the same (The link about that excercise https://leetcode.com/ | Jkize | NORMAL | 2022-07-14T20:49:41.189907+00:00 | 2022-07-14T20:50:51.163179+00:00 | 477 | false | So the solution is a little simple, \nthe first step is how to build the algorithm if two tree is the same (The link about that excercise https://leetcode.com/problems/same-tree/) \n\nThis is the code to compare if two **tree is the same** (https://leetcode.com/problems/same-tree/discuss/32687/Five-line-Java-solution... | 11 | 0 | ['Recursion', 'Java'] | 1 |
symmetric-tree | [C++] 100% Runtime Iterative Solution | c-100-runtime-iterative-solution-by-huna-xsej | \nbool isSymmetric(TreeNode* root) {\n if(!root) return true;\n queue<TreeNode*> queue;\n queue.push(root->left);\n queue.push(root- | hunarbatra | NORMAL | 2021-12-14T09:07:47.660790+00:00 | 2021-12-14T09:07:47.660819+00:00 | 847 | false | ```\nbool isSymmetric(TreeNode* root) {\n if(!root) return true;\n queue<TreeNode*> queue;\n queue.push(root->left);\n queue.push(root->right);\n while(!queue.empty()) {\n TreeNode *left = queue.front(); queue.pop();\n TreeNode *right = queue.front(); queue.pop()... | 11 | 0 | ['C', 'Iterator', 'C++'] | 1 |
symmetric-tree | Javascript - queue in 10 lines | javascript-queue-in-10-lines-by-nemtsv-rnbu | \nvar isSymmetric = function(root) {\n const q = [root, root];\n while (q.length) {\n const [l, r] = [q.shift(), q.shift()];\n if (!l && !r) continue;\n | nemtsv | NORMAL | 2019-12-09T17:47:07.234610+00:00 | 2019-12-11T00:56:03.647048+00:00 | 1,618 | false | ```\nvar isSymmetric = function(root) {\n const q = [root, root];\n while (q.length) {\n const [l, r] = [q.shift(), q.shift()];\n if (!l && !r) continue;\n if (!!l !== !!r || l.val !== r.val) return false;\n q.push(l.left, r.right, l.right, r.left);\n }\n \n return true;\n};\n\n``` | 11 | 0 | ['Queue', 'JavaScript'] | 0 |
symmetric-tree | javascript | javascript-by-yinchuhui88-c7ks | \nvar isSymmetric = function(root) {\n if(!root) \n return true;\n return dfs(root.left, root.right);\n \n function dfs(leftNode, rightNode) | yinchuhui88 | NORMAL | 2018-10-16T03:48:38.521980+00:00 | 2018-10-16T03:48:38.522027+00:00 | 2,217 | false | ```\nvar isSymmetric = function(root) {\n if(!root) \n return true;\n return dfs(root.left, root.right);\n \n function dfs(leftNode, rightNode) {\n if (!leftNode && !rightNode) {\n return true;\n }\n if(leftNode && !rightNode || !leftNode && rightNode || leftNode.val !... | 11 | 1 | [] | 1 |
symmetric-tree | Clean Java solution | clean-java-solution-by-jopiko123-7ijt | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n return isSymmetric(root, root);\n }\n \n boolean | jopiko123 | NORMAL | 2015-12-29T16:19:07+00:00 | 2015-12-29T16:19:07+00:00 | 1,445 | false | public class Solution {\n public boolean isSymmetric(TreeNode root) {\n return isSymmetric(root, root);\n }\n \n boolean isSymmetric(TreeNode n1, TreeNode n2) {\n if(n1 == null && n2 == null) return true;\n if(n1 == null || n2 == null) return false;\n ... | 11 | 0 | ['Java'] | 0 |
symmetric-tree | JAVA EASY SOLUTION BEATS 100%| 0MS | java-easy-solution-beats-100-0ms-by-saad-9j1l | IntuitionTo check if a tree is symmetric, think of folding it down the middle. If the left side is a mirror image of the right side, then the tree is symmetric. | saadiyamalan23 | NORMAL | 2025-01-17T19:15:36.236034+00:00 | 2025-01-18T09:43:25.007950+00:00 | 1,620 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To check if a tree is symmetric, think of folding it down the middle. If the left side is a mirror image of the right side, then the tree is symmetric.
For every node on the left, there should be a matching node on the right with the same ... | 10 | 0 | ['Tree', 'Depth-First Search', 'Breadth-First Search', 'Recursion', 'Binary Tree', 'Java'] | 5 |
symmetric-tree | C++ recursive solution|Easy Observation|Day 13-SUCCESSFULL|#LEETCODEDAILY | c-recursive-solutioneasy-observationday-l4yk4 | Intuition\n Describe your first thoughts on how to solve this problem. \nOn observing we clearly see we have to perform repetitive task for two roots which are | pawas48ram | NORMAL | 2023-03-13T04:46:52.976474+00:00 | 2023-03-13T04:52:16.633309+00:00 | 1,273 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOn observing we clearly see we have to perform repetitive task for two roots which are mainly the left and right part we want to compare \nSo we can divide our bigger problem into smaller problem of taking the whole tree and only compare ... | 10 | 0 | ['C++'] | 2 |
symmetric-tree | ✅✅Easy-Understand ||✅✅ recursion || ✅✅C++ | easy-understand-recursion-c-by-arpit507-2ht0 | \npublic:\n bool is_same(TreeNode *root1, TreeNode *root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) re | Arpit507 | NORMAL | 2022-10-11T19:09:02.172363+00:00 | 2022-10-22T06:34:05.926268+00:00 | 901 | false | ```\npublic:\n bool is_same(TreeNode *root1, TreeNode *root2){\n if(!root1 && !root2) return true;\n if((!root1 && root2) || (root1 && !root2)) return false;\n \n if(root1->val == root2->val) return is_same(root1->left , root2->right) && is_same(root1->right , root2->left);\n else ... | 10 | 0 | ['Recursion', 'C'] | 0 |
symmetric-tree | C++ || 4 MS || EASY TO UNDERSTAND | c-4-ms-easy-to-understand-by-bhuvisha020-02w4 | \tclass Solution {\n\tpublic:\n\t\tbool isSymmetric(TreeNode root) {\n\t\t\treturn (root==NULL || isSymmetricHelp(root->left,root->right));\n\t\t}\n\t\tbool isS | bhuvisha0208 | NORMAL | 2022-02-16T03:54:53.376613+00:00 | 2022-02-16T03:55:51.081498+00:00 | 598 | false | \tclass Solution {\n\tpublic:\n\t\tbool isSymmetric(TreeNode* root) {\n\t\t\treturn (root==NULL || isSymmetricHelp(root->left,root->right));\n\t\t}\n\t\tbool isSymmetricHelp(TreeNode*left,TreeNode*right){\n\t\t\tif(left==NULL || right==NULL)\n\t\t\t\treturn left==right;\n\t\t\tif(left->val != right->val)\n\t\t\t\tretur... | 10 | 0 | ['Recursion', 'C'] | 0 |
symmetric-tree | C# recursive 76ms, faster than 100% | c-recursive-76ms-faster-than-100-by-pawe-y6b7 | Runtime: 76 ms, faster than 100.00% of C# online submissions for Symmetric Tree.\nMemory Usage: 25.7 MB, less than 50.29% of C# online submissions for Symmetric | pawel753 | NORMAL | 2020-10-31T10:45:55.078706+00:00 | 2020-10-31T10:45:55.078738+00:00 | 1,746 | false | Runtime: 76 ms, faster than 100.00% of C# online submissions for Symmetric Tree.\nMemory Usage: 25.7 MB, less than 50.29% of C# online submissions for Symmetric Tree.\n```\npublic class Solution {\n public bool IsSymmetric(TreeNode root) => CheckSymetry(root?.left, root?.right);\n \n private bool CheckSymetry(... | 10 | 1 | ['C#'] | 1 |
symmetric-tree | In C | in-c-by-lettty17-9k5t | \n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * struct TreeNode *left;\n * struct TreeNode *right;\n * };\n */\n | lettty17 | NORMAL | 2020-05-11T01:25:09.406215+00:00 | 2020-05-11T01:25:09.406252+00:00 | 1,444 | false | ```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * struct TreeNode *left;\n * struct TreeNode *right;\n * };\n */\n\nbool parallel_traverse(struct TreeNode* a, struct TreeNode* b)\n{\n if (a == NULL && b == NULL)\n return true;\n \n if (a == NULL || b == NULL... | 10 | 0 | ['C'] | 1 |
symmetric-tree | Tree versions in Java: recursion, optimized tail recursion and pre-order iteration | tree-versions-in-java-recursion-optimize-5zvg | The most simple version is normal recursion:\n\n public class Solution {\n \tpublic boolean isSymmetric(TreeNode root) {\n \t\treturn this.isMirror(roo | vision57 | NORMAL | 2015-01-30T13:59:09+00:00 | 2015-01-30T13:59:09+00:00 | 1,466 | false | The most simple version is normal recursion:\n\n public class Solution {\n \tpublic boolean isSymmetric(TreeNode root) {\n \t\treturn this.isMirror(root, root);\n \t}\n \n \tprivate boolean isMirror(TreeNode t0, TreeNode t1) {\n\t\tif (t0 == null || t1 == null) {\n\t\t\treturn t0 == t1;\n\t\t}\n\t\tre... | 10 | 0 | [] | 3 |
symmetric-tree | A simple python recursive solution - O(n) 60ms | a-simple-python-recursive-solution-on-60-iagy | # Definition for a binary tree node.\n # class TreeNode:\n # def __init__(self, x):\n # self.val = x\n # self.left = None\n # | google | NORMAL | 2015-05-24T20:16:05+00:00 | 2015-05-24T20:16:05+00:00 | 2,098 | false | # Definition for a binary tree node.\n # class TreeNode:\n # def __init__(self, x):\n # self.val = x\n # self.left = None\n # self.right = None\n \n class Solution:\n # @param {TreeNode} root\n # @return {boolean}\n def isSymmetric(self, root):\n... | 10 | 0 | ['Python'] | 4 |
symmetric-tree | Recursive and non-recursive solutions in Python | recursive-and-non-recursive-solutions-in-vfb6 | Recursive solution (68ms):\n\n class Solution(object):\n def isSymmetric(self, root):\n if not root:\n return True\n | hweicdl | NORMAL | 2015-11-18T12:07:02+00:00 | 2018-09-21T19:35:15.781517+00:00 | 1,053 | false | Recursive solution (68ms):\n\n class Solution(object):\n def isSymmetric(self, root):\n if not root:\n return True\n return self.equals(root.left, root.right)\n \n def equals(self, node1, node2):\n if not node1 and not node2:\n retur... | 10 | 0 | ['Recursion'] | 2 |
symmetric-tree | My AC Code ,is there a better method ? | my-ac-code-is-there-a-better-method-by-l-rsz5 | public boolean checkSymmetric(TreeNode lsubTree,TreeNode rsubTree){\n if(lsubTree==null&&rsubTree==null) return true;\n else if(lsubTree!=null&&r | lzklee | NORMAL | 2014-03-03T08:05:05+00:00 | 2014-03-03T08:05:05+00:00 | 2,080 | false | public boolean checkSymmetric(TreeNode lsubTree,TreeNode rsubTree){\n if(lsubTree==null&&rsubTree==null) return true;\n else if(lsubTree!=null&&rsubTree==null) return false;\n else if(lsubTree==null&&rsubTree!=null) return false;\n else if(lsubTree.val!=rsubTree.val) return false;\n ... | 10 | 0 | [] | 2 |
symmetric-tree | 101. Symmetric Tree | JS | 101-symmetric-tree-js-by-ahmedaboelfadle-ijio | Intuition\n Describe your first thoughts on how to solve this problem. \n- At first i was thinking to solve it with iterative approach, but it would be costly t | AhmedAboElfadle | NORMAL | 2022-12-19T09:35:53.834061+00:00 | 2022-12-19T09:47:57.172378+00:00 | 1,864 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- At first i was thinking to solve it with iterative approach, but it would be costly to use a queue for this, so i thought to use the recursive approach.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- We use re... | 9 | 0 | ['Tree', 'Recursion', 'Binary Tree', 'JavaScript'] | 1 |
symmetric-tree | C++ 7ms shortest solution | c-7ms-shortest-solution-by-nilesh_5-ao85 | This question is just an another part of checking two same trees.we treat both left and right subtrees as different. Here we will need to compare left value of | Nilesh_5 | NORMAL | 2022-08-18T16:42:51.548005+00:00 | 2022-08-18T16:42:51.548035+00:00 | 622 | false | This question is just an another part of checking two same trees.we treat both left and right subtrees as different. Here we will need to compare left value of one tree with right value of another tree.\n```\nclass Solution {\nprivate:\n bool isSameTree(TreeNode *p, TreeNode *q) {\n if (p == NULL || q == NULL... | 9 | 0 | ['Recursion', 'C', 'C++'] | 0 |
symmetric-tree | Clean and Easy Recursive and Iterative solutions | clean-and-easy-recursive-and-iterative-s-ncba | Recursive\n\n\nvar isSymmetric = function(root) {\n \n const helper = (node1, node2) => {\n if(node1 === null && node2 === null)\n retur | dollysingh | NORMAL | 2022-05-09T18:00:41.920508+00:00 | 2022-05-09T18:00:41.920534+00:00 | 882 | false | **Recursive**\n\n```\nvar isSymmetric = function(root) {\n \n const helper = (node1, node2) => {\n if(node1 === null && node2 === null)\n return true;\n \n if(node1 === null || node2 === null)\n return false;\n \n if(node1.val === node2.val) {\n ... | 9 | 0 | ['Recursion', 'Iterator', 'JavaScript'] | 1 |
symmetric-tree | python | iterative | recursive | python-iterative-recursive-by-fatemebesh-la26 | recursive\n\n\tclass Solution:\n\t\tdef isSymmetric(self, root):\n\t\t\tif not root:\n\t\t\t\treturn False\n\t\t\tdef helper(l, r):\n\t\t\t\tif not l and not r: | fatemebesharat766 | NORMAL | 2021-12-12T17:24:26.728505+00:00 | 2021-12-12T17:48:02.518608+00:00 | 699 | false | recursive\n\n\tclass Solution:\n\t\tdef isSymmetric(self, root):\n\t\t\tif not root:\n\t\t\t\treturn False\n\t\t\tdef helper(l, r):\n\t\t\t\tif not l and not r:\n\t\t\t\t\treturn True\n\t\t\t\tif l and r and l.val == r.val:\n\t\t\t\t\treturn helper(l.left, r.right) and helper(l.right, r.left)\n\n\t\t\treturn helper(roo... | 9 | 1 | ['Recursion', 'Iterator', 'Python', 'Python3'] | 1 |
symmetric-tree | Python 3 simple recursion, faster than 97% and less memory than 100% | python-3-simple-recursion-faster-than-97-49qd | \n\n\nclass Solution:\n def isSymmetric(self, root: TreeNode) -> bool:\n \n if not root:\n return True\n \n left=root. | claret | NORMAL | 2020-01-14T01:40:47.351117+00:00 | 2020-01-14T01:40:47.351167+00:00 | 1,730 | false | \n\n```\nclass Solution:\n def isSymmetric(self, root: TreeNode) -> bool:\n \n if not root:\n return True\n \n left=root.left\n right=root.right\n \n if not left and not right:\n return True\n \n def check(t1, t2):\n if n... | 9 | 0 | ['Python', 'Python3'] | 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.