We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bf6bdeb commit 0ba3e14Copy full SHA for 0ba3e14
same-tree/sora0319.java
@@ -0,0 +1,26 @@
1
+public class Solution {
2
+ public boolean isSameTree(TreeNode p, TreeNode q) {
3
+ Stack<TreeNode[]> stack = new Stack<>();
4
+ stack.push(new TreeNode[]{p, q});
5
+
6
+ while (!stack.isEmpty()) {
7
+ TreeNode[] nodes = stack.pop();
8
+ TreeNode n1 = nodes[0];
9
+ TreeNode n2 = nodes[1];
10
11
+ if (n1 == null && n2 == null) {
12
+ continue;
13
+ }
14
+ if (n1 == null || n2 == null) {
15
+ return false;
16
17
+ if (n1.val != n2.val) {
18
19
20
+ stack.push(new TreeNode[]{n1.left, n2.left});
21
+ stack.push(new TreeNode[]{n1.right, n2.right});
22
23
+ return true;
24
25
+}
26
0 commit comments