From 2f8a03ef4bcf6176b1088347270bde644577792e Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 17 May 2021 13:06:23 +0800 Subject: [PATCH] =?UTF-8?q?993:=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84?= =?UTF-8?q?=E5=A0=82=E5=85=84=E5=BC=9F=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/CousinsInBinaryTree.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/leetcode/editor/cn/CousinsInBinaryTree.java b/src/main/java/leetcode/editor/cn/CousinsInBinaryTree.java index b1a5905..2362dc1 100644 --- a/src/main/java/leetcode/editor/cn/CousinsInBinaryTree.java +++ b/src/main/java/leetcode/editor/cn/CousinsInBinaryTree.java @@ -75,11 +75,11 @@ public class CousinsInBinaryTree { */ class Solution { public boolean isCousins(TreeNode root, int x, int y) { - int[][] paraent = getParent(root, x, y, new int[2][2], 1); + int[][] paraent = getParent(root, x, y, new int[2][2], 1,-1); return paraent[0][0] != paraent[1][0] && paraent[0][1] == paraent[1][1]; } - private int[][] getParent(TreeNode root, int x, int y, int[][] paraent, int deep) { + private int[][] getParent(TreeNode root, int x, int y, int[][] paraent, int deep,int before) { if (paraent[1][1] > 0) { return paraent; } @@ -87,21 +87,21 @@ public class CousinsInBinaryTree { x = root.val == x ? 0 : x; y = root.val == y ? 0 : y; if (paraent[0][1] > 0) { - paraent[1][0] = root.val; + paraent[1][0] = before; paraent[1][1] = deep; } else { - paraent[0][0] = root.val; + paraent[0][0] = before; paraent[0][1] = deep; } } if (root.left != null) { - paraent = getParent(root.left, x, y, paraent, deep + 1); + paraent = getParent(root.left, x, y, paraent, deep + 1,root.val); } if (paraent[1][1] > 0) { return paraent; } if (root.right != null) { - paraent = getParent(root.right, x, y, paraent, deep + 1); + paraent = getParent(root.right, x, y, paraent, deep + 1,root.val); } return paraent; }