diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/README.md b/solution/0900-0999/0993.Cousins in Binary Tree/README.md index 9ac4faa5d54d5..1be0495c66ec7 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/README.md +++ b/solution/0900-0999/0993.Cousins in Binary Tree/README.md @@ -75,21 +75,21 @@ class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: q = deque([(root, None)]) - d = 0 + depth = 0 p1 = p2 = None - d1 = d2 = 0 + d1 = d2 = None while q: for _ in range(len(q)): - node, fa = q.popleft() + node, parent = q.popleft() if node.val == x: - p1, d1 = fa, d - if node.val == y: - p2, d2 = fa, d + p1, d1 = parent, depth + elif node.val == y: + p2, d2 = parent, depth if node.left: q.append((node.left, node)) if node.right: q.append((node.right, node)) - d += 1 + depth += 1 return p1 != p2 and d1 == d2 ``` @@ -111,22 +111,20 @@ class Solution: */ class Solution { public boolean isCousins(TreeNode root, int x, int y) { - TreeNode p1 = null, p2 = null; - int d1 = 0, d2 = 0; Deque q = new ArrayDeque<>(); q.offer(new TreeNode[] {root, null}); - int d = 0; - while (!q.isEmpty()) { + int d1 = 0, d2 = 0; + TreeNode p1 = null, p2 = null; + for (int depth = 0; !q.isEmpty(); ++depth) { for (int n = q.size(); n > 0; --n) { - var p = q.poll(); - TreeNode node = p[0], fa = p[1]; + TreeNode[] t = q.poll(); + TreeNode node = t[0], parent = t[1]; if (node.val == x) { - p1 = fa; - d1 = d; - } - if (node.val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node.val == y) { + d2 = depth; + p2 = parent; } if (node.left != null) { q.offer(new TreeNode[] {node.left, node}); @@ -135,7 +133,6 @@ class Solution { q.offer(new TreeNode[] {node.right, node}); } } - ++d; } return p1 != p2 && d1 == d2; } @@ -157,34 +154,30 @@ class Solution { class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode* p1 = nullptr; - TreeNode* p2 = nullptr; - int d1 = 0, d2 = 0; queue> q; - q.emplace(root, nullptr); - int d = 0; - while (!q.empty()) { + q.push({root, nullptr}); + int d1 = 0, d2 = 0; + TreeNode *p1 = nullptr, *p2 = nullptr; + for (int depth = 0; q.size(); ++depth) { for (int n = q.size(); n; --n) { - auto [node, fa] = q.front(); + auto [node, parent] = q.front(); q.pop(); if (node->val == x) { - p1 = fa; - d1 = d; - } - if (node->val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node->val == y) { + d2 = depth; + p2 = parent; } if (node->left) { - q.emplace(node->left, node); + q.push({node->left, node}); } if (node->right) { - q.emplace(node->right, node); + q.push({node->right, node}); } } - ++d; } - return p1 != p2 && d1 == d2; + return d1 == d2 && p1 != p2; } }; ``` @@ -199,20 +192,18 @@ public: * } */ func isCousins(root *TreeNode, x int, y int) bool { - type pair struct{ node, fa *TreeNode } - q := []pair{pair{root, nil}} + type pair struct{ node, parent *TreeNode } + var d1, d2 int var p1, p2 *TreeNode - var d, d1, d2 int - for len(q) > 0 { + q := []pair{{root, nil}} + for depth := 0; len(q) > 0; depth++ { for n := len(q); n > 0; n-- { - p := q[0] + node, parent := q[0].node, q[0].parent q = q[1:] - node, fa := p.node, p.fa if node.Val == x { - p1, d1 = fa, d - } - if node.Val == y { - p2, d2 = fa, d + d1, p1 = depth, parent + } else if node.Val == y { + d2, p2 = depth, parent } if node.Left != nil { q = append(q, pair{node.Left, node}) @@ -221,9 +212,48 @@ func isCousins(root *TreeNode, x int, y int) bool { q = append(q, pair{node.Right, node}) } } - d++ } - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 +} +``` + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2] = [0, 0]; + let [p1, p2] = [null, null]; + const q: [TreeNode, TreeNode][] = [[root, null]]; + for (let depth = 0; q.length > 0; ++depth) { + const t: [TreeNode, TreeNode][] = []; + for (const [node, parent] of q) { + if (node.val === x) { + [d1, p1] = [depth, parent]; + } else if (node.val === y) { + [d2, p2] = [depth, parent]; + } + if (node.left) { + t.push([node.left, node]); + } + if (node.right) { + t.push([node.right, node]); + } + } + q.splice(0, q.length, ...t); + } + return d1 === d2 && p1 !== p2; } ``` @@ -231,9 +261,9 @@ func isCousins(root *TreeNode, x int, y int) bool { ### 方法二:DFS -我们设计一个函数 $dfs(root, fa, d)$,表示从根节点 $root$ 出发,其父节点为 $fa$,深度为 $d$,进行深度优先搜索。 +我们设计一个函数 $dfs(root, parent, depth)$,表示从根节点 $root$ 出发,其父节点为 $parent$,深度为 $depth$,进行深度优先搜索。 -在函数中,我们首先判断当前节点是否为空,如果为空,则直接返回。如果当前节点的值为 $x$ 或 $y$,则记录该节点的父节点和深度。然后对当前节点的左右子节点分别调用函数 $dfs$,其中父节点为当前节点,深度为当前深度加 $1$。即 $dfs(root.left, root, d + 1)$ 和 $dfs(root.right, root, d + 1)$。 +在函数中,我们首先判断当前节点是否为空,如果为空,则直接返回。如果当前节点的值为 $x$ 或 $y$,则记录该节点的父节点和深度。然后对当前节点的左右子节点分别调用函数 $dfs$,其中父节点为当前节点,深度为当前深度加 $1$。即 $dfs(root.left, root, depth + 1)$ 和 $dfs(root.right, root, depth + 1)$。 当整棵二叉树遍历完毕后,如果 $x$ 和 $y$ 的深度相同且父节点不同,则返回 $true$,否则返回 $false$。 @@ -250,19 +280,19 @@ func isCousins(root *TreeNode, x int, y int) bool { # self.right = right class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: - def dfs(root, fa, d): + def dfs(root, parent, depth): if root is None: return if root.val == x: - t[0] = (fa, d) - if root.val == y: - t[1] = (fa, d) - dfs(root.left, root, d + 1) - dfs(root.right, root, d + 1) + st[0] = (parent, depth) + elif root.val == y: + st[1] = (parent, depth) + dfs(root.left, root, depth + 1) + dfs(root.right, root, depth + 1) - t = [None, None] + st = [None, None] dfs(root, None, 0) - return t[0][0] != t[1][0] and t[0][1] == t[1][1] + return st[0][0] != st[1][0] and st[0][1] == st[1][1] ``` ```java @@ -283,8 +313,8 @@ class Solution: */ class Solution { private int x, y; - private TreeNode p1, p2; private int d1, d2; + private TreeNode p1, p2; public boolean isCousins(TreeNode root, int x, int y) { this.x = x; @@ -293,20 +323,19 @@ class Solution { return p1 != p2 && d1 == d2; } - private void dfs(TreeNode root, TreeNode p, int d) { + private void dfs(TreeNode root, TreeNode parent, int depth) { if (root == null) { return; } if (root.val == x) { - p1 = p; - d1 = d; - } - if (root.val == y) { - p2 = p; - d2 = d; + d1 = depth; + p1 = parent; + } else if (root.val == y) { + d2 = depth; + p2 = parent; } - dfs(root.left, root, d + 1); - dfs(root.right, root, d + 1); + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); } } ``` @@ -326,22 +355,22 @@ class Solution { class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode *p1, *p2; int d1, d2; - function dfs = [&](TreeNode* root, TreeNode* fa, int d) { + TreeNode* p1; + TreeNode* p2; + function dfs = [&](TreeNode* root, TreeNode* parent, int depth) { if (!root) { return; } if (root->val == x) { - p1 = fa; - d1 = d; + d1 = depth; + p1 = parent; + } else if (root->val == y) { + d2 = depth; + p2 = parent; } - if (root->val == y) { - p2 = fa; - d2 = d; - } - dfs(root->left, root, d + 1); - dfs(root->right, root, d + 1); + dfs(root->left, root, depth + 1); + dfs(root->right, root, depth + 1); }; dfs(root, nullptr, 0); return p1 != p2 && d1 == d2; @@ -359,24 +388,57 @@ public: * } */ func isCousins(root *TreeNode, x int, y int) bool { - var p1, p2 *TreeNode var d1, d2 int - var dfs func(*TreeNode, *TreeNode, int) - dfs = func(root *TreeNode, fa *TreeNode, d int) { + var p1, p2 *TreeNode + var dfs func(root, parent *TreeNode, depth int) + dfs = func(root, parent *TreeNode, depth int) { if root == nil { return } if root.Val == x { - p1, d1 = fa, d + d1, p1 = depth, parent + } else if root.Val == y { + d2, p2 = depth, parent } - if root.Val == y { - p2, d2 = fa, d - } - dfs(root.Left, root, d+1) - dfs(root.Right, root, d+1) + dfs(root.Left, root, depth+1) + dfs(root.Right, root, depth+1) } dfs(root, nil, 0) - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 +} +``` + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2, p1, p2] = [0, 0, null, null]; + const dfs = (root: TreeNode | null, parent: TreeNode | null, depth: number) => { + if (!root) { + return; + } + if (root.val === x) { + [d1, p1] = [depth, parent]; + } else if (root.val === y) { + [d2, p2] = [depth, parent]; + } + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); + }; + dfs(root, null, 0); + return d1 === d2 && p1 !== p2; } ``` diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md b/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md index fe84a0ff5e019..25979cba9b8a5 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md +++ b/solution/0900-0999/0993.Cousins in Binary Tree/README_EN.md @@ -59,21 +59,21 @@ class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: q = deque([(root, None)]) - d = 0 + depth = 0 p1 = p2 = None - d1 = d2 = 0 + d1 = d2 = None while q: for _ in range(len(q)): - node, fa = q.popleft() + node, parent = q.popleft() if node.val == x: - p1, d1 = fa, d - if node.val == y: - p2, d2 = fa, d + p1, d1 = parent, depth + elif node.val == y: + p2, d2 = parent, depth if node.left: q.append((node.left, node)) if node.right: q.append((node.right, node)) - d += 1 + depth += 1 return p1 != p2 and d1 == d2 ``` @@ -95,22 +95,20 @@ class Solution: */ class Solution { public boolean isCousins(TreeNode root, int x, int y) { - TreeNode p1 = null, p2 = null; - int d1 = 0, d2 = 0; Deque q = new ArrayDeque<>(); q.offer(new TreeNode[] {root, null}); - int d = 0; - while (!q.isEmpty()) { + int d1 = 0, d2 = 0; + TreeNode p1 = null, p2 = null; + for (int depth = 0; !q.isEmpty(); ++depth) { for (int n = q.size(); n > 0; --n) { - var p = q.poll(); - TreeNode node = p[0], fa = p[1]; + TreeNode[] t = q.poll(); + TreeNode node = t[0], parent = t[1]; if (node.val == x) { - p1 = fa; - d1 = d; - } - if (node.val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node.val == y) { + d2 = depth; + p2 = parent; } if (node.left != null) { q.offer(new TreeNode[] {node.left, node}); @@ -119,7 +117,6 @@ class Solution { q.offer(new TreeNode[] {node.right, node}); } } - ++d; } return p1 != p2 && d1 == d2; } @@ -141,34 +138,30 @@ class Solution { class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode* p1 = nullptr; - TreeNode* p2 = nullptr; - int d1 = 0, d2 = 0; queue> q; - q.emplace(root, nullptr); - int d = 0; - while (!q.empty()) { + q.push({root, nullptr}); + int d1 = 0, d2 = 0; + TreeNode *p1 = nullptr, *p2 = nullptr; + for (int depth = 0; q.size(); ++depth) { for (int n = q.size(); n; --n) { - auto [node, fa] = q.front(); + auto [node, parent] = q.front(); q.pop(); if (node->val == x) { - p1 = fa; - d1 = d; - } - if (node->val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node->val == y) { + d2 = depth; + p2 = parent; } if (node->left) { - q.emplace(node->left, node); + q.push({node->left, node}); } if (node->right) { - q.emplace(node->right, node); + q.push({node->right, node}); } } - ++d; } - return p1 != p2 && d1 == d2; + return d1 == d2 && p1 != p2; } }; ``` @@ -183,20 +176,18 @@ public: * } */ func isCousins(root *TreeNode, x int, y int) bool { - type pair struct{ node, fa *TreeNode } - q := []pair{pair{root, nil}} + type pair struct{ node, parent *TreeNode } + var d1, d2 int var p1, p2 *TreeNode - var d, d1, d2 int - for len(q) > 0 { + q := []pair{{root, nil}} + for depth := 0; len(q) > 0; depth++ { for n := len(q); n > 0; n-- { - p := q[0] + node, parent := q[0].node, q[0].parent q = q[1:] - node, fa := p.node, p.fa if node.Val == x { - p1, d1 = fa, d - } - if node.Val == y { - p2, d2 = fa, d + d1, p1 = depth, parent + } else if node.Val == y { + d2, p2 = depth, parent } if node.Left != nil { q = append(q, pair{node.Left, node}) @@ -205,9 +196,48 @@ func isCousins(root *TreeNode, x int, y int) bool { q = append(q, pair{node.Right, node}) } } - d++ } - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 +} +``` + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2] = [0, 0]; + let [p1, p2] = [null, null]; + const q: [TreeNode, TreeNode][] = [[root, null]]; + for (let depth = 0; q.length > 0; ++depth) { + const t: [TreeNode, TreeNode][] = []; + for (const [node, parent] of q) { + if (node.val === x) { + [d1, p1] = [depth, parent]; + } else if (node.val === y) { + [d2, p2] = [depth, parent]; + } + if (node.left) { + t.push([node.left, node]); + } + if (node.right) { + t.push([node.right, node]); + } + } + q.splice(0, q.length, ...t); + } + return d1 === d2 && p1 !== p2; } ``` @@ -226,19 +256,19 @@ func isCousins(root *TreeNode, x int, y int) bool { # self.right = right class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: - def dfs(root, fa, d): + def dfs(root, parent, depth): if root is None: return if root.val == x: - t[0] = (fa, d) - if root.val == y: - t[1] = (fa, d) - dfs(root.left, root, d + 1) - dfs(root.right, root, d + 1) + st[0] = (parent, depth) + elif root.val == y: + st[1] = (parent, depth) + dfs(root.left, root, depth + 1) + dfs(root.right, root, depth + 1) - t = [None, None] + st = [None, None] dfs(root, None, 0) - return t[0][0] != t[1][0] and t[0][1] == t[1][1] + return st[0][0] != st[1][0] and st[0][1] == st[1][1] ``` ```java @@ -259,8 +289,8 @@ class Solution: */ class Solution { private int x, y; - private TreeNode p1, p2; private int d1, d2; + private TreeNode p1, p2; public boolean isCousins(TreeNode root, int x, int y) { this.x = x; @@ -269,20 +299,19 @@ class Solution { return p1 != p2 && d1 == d2; } - private void dfs(TreeNode root, TreeNode p, int d) { + private void dfs(TreeNode root, TreeNode parent, int depth) { if (root == null) { return; } if (root.val == x) { - p1 = p; - d1 = d; - } - if (root.val == y) { - p2 = p; - d2 = d; + d1 = depth; + p1 = parent; + } else if (root.val == y) { + d2 = depth; + p2 = parent; } - dfs(root.left, root, d + 1); - dfs(root.right, root, d + 1); + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); } } ``` @@ -302,22 +331,22 @@ class Solution { class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode *p1, *p2; int d1, d2; - function dfs = [&](TreeNode* root, TreeNode* fa, int d) { + TreeNode* p1; + TreeNode* p2; + function dfs = [&](TreeNode* root, TreeNode* parent, int depth) { if (!root) { return; } if (root->val == x) { - p1 = fa; - d1 = d; + d1 = depth; + p1 = parent; + } else if (root->val == y) { + d2 = depth; + p2 = parent; } - if (root->val == y) { - p2 = fa; - d2 = d; - } - dfs(root->left, root, d + 1); - dfs(root->right, root, d + 1); + dfs(root->left, root, depth + 1); + dfs(root->right, root, depth + 1); }; dfs(root, nullptr, 0); return p1 != p2 && d1 == d2; @@ -335,24 +364,57 @@ public: * } */ func isCousins(root *TreeNode, x int, y int) bool { - var p1, p2 *TreeNode var d1, d2 int - var dfs func(*TreeNode, *TreeNode, int) - dfs = func(root *TreeNode, fa *TreeNode, d int) { + var p1, p2 *TreeNode + var dfs func(root, parent *TreeNode, depth int) + dfs = func(root, parent *TreeNode, depth int) { if root == nil { return } if root.Val == x { - p1, d1 = fa, d + d1, p1 = depth, parent + } else if root.Val == y { + d2, p2 = depth, parent } - if root.Val == y { - p2, d2 = fa, d - } - dfs(root.Left, root, d+1) - dfs(root.Right, root, d+1) + dfs(root.Left, root, depth+1) + dfs(root.Right, root, depth+1) } dfs(root, nil, 0) - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 +} +``` + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2, p1, p2] = [0, 0, null, null]; + const dfs = (root: TreeNode | null, parent: TreeNode | null, depth: number) => { + if (!root) { + return; + } + if (root.val === x) { + [d1, p1] = [depth, parent]; + } else if (root.val === y) { + [d2, p2] = [depth, parent]; + } + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); + }; + dfs(root, null, 0); + return d1 === d2 && p1 !== p2; } ``` diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp index ec80a4f74591e..c759d1d2e15ca 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp @@ -12,33 +12,29 @@ class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode* p1 = nullptr; - TreeNode* p2 = nullptr; - int d1 = 0, d2 = 0; queue> q; - q.emplace(root, nullptr); - int d = 0; - while (!q.empty()) { + q.push({root, nullptr}); + int d1 = 0, d2 = 0; + TreeNode *p1 = nullptr, *p2 = nullptr; + for (int depth = 0; q.size(); ++depth) { for (int n = q.size(); n; --n) { - auto [node, fa] = q.front(); + auto [node, parent] = q.front(); q.pop(); if (node->val == x) { - p1 = fa; - d1 = d; - } - if (node->val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node->val == y) { + d2 = depth; + p2 = parent; } if (node->left) { - q.emplace(node->left, node); + q.push({node->left, node}); } if (node->right) { - q.emplace(node->right, node); + q.push({node->right, node}); } } - ++d; } - return p1 != p2 && d1 == d2; + return d1 == d2 && p1 != p2; } }; \ No newline at end of file diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.go b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.go index 2de89e76477ec..606762f9fec38 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.go +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.go @@ -7,20 +7,18 @@ * } */ func isCousins(root *TreeNode, x int, y int) bool { - type pair struct{ node, fa *TreeNode } - q := []pair{pair{root, nil}} + type pair struct{ node, parent *TreeNode } + var d1, d2 int var p1, p2 *TreeNode - var d, d1, d2 int - for len(q) > 0 { + q := []pair{{root, nil}} + for depth := 0; len(q) > 0; depth++ { for n := len(q); n > 0; n-- { - p := q[0] + node, parent := q[0].node, q[0].parent q = q[1:] - node, fa := p.node, p.fa if node.Val == x { - p1, d1 = fa, d - } - if node.Val == y { - p2, d2 = fa, d + d1, p1 = depth, parent + } else if node.Val == y { + d2, p2 = depth, parent } if node.Left != nil { q = append(q, pair{node.Left, node}) @@ -29,7 +27,6 @@ func isCousins(root *TreeNode, x int, y int) bool { q = append(q, pair{node.Right, node}) } } - d++ } - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 } \ No newline at end of file diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.java b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.java index 527b0c1ff194d..68d9549cc92cd 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.java +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.java @@ -15,22 +15,20 @@ */ class Solution { public boolean isCousins(TreeNode root, int x, int y) { - TreeNode p1 = null, p2 = null; - int d1 = 0, d2 = 0; Deque q = new ArrayDeque<>(); q.offer(new TreeNode[] {root, null}); - int d = 0; - while (!q.isEmpty()) { + int d1 = 0, d2 = 0; + TreeNode p1 = null, p2 = null; + for (int depth = 0; !q.isEmpty(); ++depth) { for (int n = q.size(); n > 0; --n) { - var p = q.poll(); - TreeNode node = p[0], fa = p[1]; + TreeNode[] t = q.poll(); + TreeNode node = t[0], parent = t[1]; if (node.val == x) { - p1 = fa; - d1 = d; - } - if (node.val == y) { - p2 = fa; - d2 = d; + d1 = depth; + p1 = parent; + } else if (node.val == y) { + d2 = depth; + p2 = parent; } if (node.left != null) { q.offer(new TreeNode[] {node.left, node}); @@ -39,7 +37,6 @@ public boolean isCousins(TreeNode root, int x, int y) { q.offer(new TreeNode[] {node.right, node}); } } - ++d; } return p1 != p2 && d1 == d2; } diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py index ec3455c189510..447f1f5ca335f 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.py @@ -7,19 +7,19 @@ class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: q = deque([(root, None)]) - d = 0 + depth = 0 p1 = p2 = None - d1 = d2 = 0 + d1 = d2 = None while q: for _ in range(len(q)): - node, fa = q.popleft() + node, parent = q.popleft() if node.val == x: - p1, d1 = fa, d - if node.val == y: - p2, d2 = fa, d + p1, d1 = parent, depth + elif node.val == y: + p2, d2 = parent, depth if node.left: q.append((node.left, node)) if node.right: q.append((node.right, node)) - d += 1 + depth += 1 return p1 != p2 and d1 == d2 diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.ts b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.ts new file mode 100644 index 0000000000000..cfe12f5e9f96b --- /dev/null +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.ts @@ -0,0 +1,37 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2] = [0, 0]; + let [p1, p2] = [null, null]; + const q: [TreeNode, TreeNode][] = [[root, null]]; + for (let depth = 0; q.length > 0; ++depth) { + const t: [TreeNode, TreeNode][] = []; + for (const [node, parent] of q) { + if (node.val === x) { + [d1, p1] = [depth, parent]; + } else if (node.val === y) { + [d2, p2] = [depth, parent]; + } + if (node.left) { + t.push([node.left, node]); + } + if (node.right) { + t.push([node.right, node]); + } + } + q.splice(0, q.length, ...t); + } + return d1 === d2 && p1 !== p2; +} diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.cpp b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.cpp index 7fe390f57cb15..846d545f76a56 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.cpp +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.cpp @@ -12,22 +12,22 @@ class Solution { public: bool isCousins(TreeNode* root, int x, int y) { - TreeNode *p1, *p2; int d1, d2; - function dfs = [&](TreeNode* root, TreeNode* fa, int d) { + TreeNode* p1; + TreeNode* p2; + function dfs = [&](TreeNode* root, TreeNode* parent, int depth) { if (!root) { return; } if (root->val == x) { - p1 = fa; - d1 = d; + d1 = depth; + p1 = parent; + } else if (root->val == y) { + d2 = depth; + p2 = parent; } - if (root->val == y) { - p2 = fa; - d2 = d; - } - dfs(root->left, root, d + 1); - dfs(root->right, root, d + 1); + dfs(root->left, root, depth + 1); + dfs(root->right, root, depth + 1); }; dfs(root, nullptr, 0); return p1 != p2 && d1 == d2; diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.go b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.go index 12f092ff1f0e5..f12b4fcd41863 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.go +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.go @@ -7,22 +7,21 @@ * } */ func isCousins(root *TreeNode, x int, y int) bool { - var p1, p2 *TreeNode var d1, d2 int - var dfs func(*TreeNode, *TreeNode, int) - dfs = func(root *TreeNode, fa *TreeNode, d int) { + var p1, p2 *TreeNode + var dfs func(root, parent *TreeNode, depth int) + dfs = func(root, parent *TreeNode, depth int) { if root == nil { return } if root.Val == x { - p1, d1 = fa, d - } - if root.Val == y { - p2, d2 = fa, d + d1, p1 = depth, parent + } else if root.Val == y { + d2, p2 = depth, parent } - dfs(root.Left, root, d+1) - dfs(root.Right, root, d+1) + dfs(root.Left, root, depth+1) + dfs(root.Right, root, depth+1) } dfs(root, nil, 0) - return p1 != p2 && d1 == d2 + return d1 == d2 && p1 != p2 } \ No newline at end of file diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.java b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.java index ff70cfd8bd2f3..15c941885b064 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.java +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.java @@ -15,8 +15,8 @@ */ class Solution { private int x, y; - private TreeNode p1, p2; private int d1, d2; + private TreeNode p1, p2; public boolean isCousins(TreeNode root, int x, int y) { this.x = x; @@ -25,19 +25,18 @@ public boolean isCousins(TreeNode root, int x, int y) { return p1 != p2 && d1 == d2; } - private void dfs(TreeNode root, TreeNode p, int d) { + private void dfs(TreeNode root, TreeNode parent, int depth) { if (root == null) { return; } if (root.val == x) { - p1 = p; - d1 = d; - } - if (root.val == y) { - p2 = p; - d2 = d; + d1 = depth; + p1 = parent; + } else if (root.val == y) { + d2 = depth; + p2 = parent; } - dfs(root.left, root, d + 1); - dfs(root.right, root, d + 1); + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); } } \ No newline at end of file diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.py b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.py index ffd5ca0f0b3a4..899eb13b77f47 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.py +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.py @@ -6,16 +6,16 @@ # self.right = right class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: - def dfs(root, fa, d): + def dfs(root, parent, depth): if root is None: return if root.val == x: - t[0] = (fa, d) - if root.val == y: - t[1] = (fa, d) - dfs(root.left, root, d + 1) - dfs(root.right, root, d + 1) + st[0] = (parent, depth) + elif root.val == y: + st[1] = (parent, depth) + dfs(root.left, root, depth + 1) + dfs(root.right, root, depth + 1) - t = [None, None] + st = [None, None] dfs(root, None, 0) - return t[0][0] != t[1][0] and t[0][1] == t[1][1] + return st[0][0] != st[1][0] and st[0][1] == st[1][1] diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.ts b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.ts new file mode 100644 index 0000000000000..f902a8299db40 --- /dev/null +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution2.ts @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function isCousins(root: TreeNode | null, x: number, y: number): boolean { + let [d1, d2, p1, p2] = [0, 0, null, null]; + const dfs = (root: TreeNode | null, parent: TreeNode | null, depth: number) => { + if (!root) { + return; + } + if (root.val === x) { + [d1, p1] = [depth, parent]; + } else if (root.val === y) { + [d2, p2] = [depth, parent]; + } + dfs(root.left, root, depth + 1); + dfs(root.right, root, depth + 1); + }; + dfs(root, null, 0); + return d1 === d2 && p1 !== p2; +}