Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/100_SameTree100.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory79 lines (74 loc) · 2.04 KB masterBreadcrumbsLeetcode_Java/100_SameTree100.javaCopy pathTopFile metadata and controlsCodeBlame79 lines (74 loc) · 2.04 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879/** * 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. * * 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 * */ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class SameTree100 { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null || p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } /** * https://leetcode.com/problems/same-tree/discuss/32684/My-non-recursive-method */ public boolean isSameTree2(TreeNode p, TreeNode q) { Stack<TreeNode> stack_p = new Stack <> (); Stack<TreeNode> stack_q = new Stack <> (); if (p != null) stack_p.push( p ) ; if (q != null) stack_q.push( q ) ; while (!stack_p.isEmpty() && !stack_q.isEmpty()) { TreeNode pn = stack_p.pop() ; TreeNode qn = stack_q.pop() ; if (pn.val != qn.val) return false ; if (pn.right != null) stack_p.push(pn.right) ; if (qn.right != null) stack_q.push(qn.right) ; if (stack_p.size() != stack_q.size()) return false ; if (pn.left != null) stack_p.push(pn.left) ; if (qn.left != null) stack_q.push(qn.left) ; if (stack_p.size() != stack_q.size()) return false ; } return stack_p.size() == stack_q.size() ; } } You can’t perform that action at this time.