LeetCode - Easy - 993. Cousins in Binary Tree

喜欢ヅ旅行 2022-10-05 09:48 125阅读 0赞

Topic

  • Tree
  • Breadth-first Search
  • Depth-first Search

Description

https://leetcode.com/problems/cousins-in-binary-tree/

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and y are cousins.

Example 1:

d38789084a1b97428f0987304b0f0182.png

  1. Input: root = [1,2,3,4], x = 4, y = 3
  2. Output: false

Example 2:

81a1925728a3e6d04c3c05bddda1630c.png

  1. Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
  2. Output: true

Example 3:

5fbda38597d33da62b878227e715aa2a.png

  1. Input: root = [1,2,3,null,4], x = 2, y = 3
  2. Output: false

Constraints:

  • The number of nodes in the tree will be between 2 and 100.
  • Each node has a unique integer value from 1 to 100.

Analysis

方法一:BFS

方法二:DFS

Submission

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class CousinsInBinaryTree {
  6. //方法一:BFS
  7. public boolean isCousins(TreeNode root, int x, int y) {
  8. LinkedList<TreeNode> queue = new LinkedList<>();
  9. List<Integer> tempList = new ArrayList<>();
  10. queue.offer(root);
  11. int depth = 0;
  12. checkNode(null, root, x, y, depth, tempList);
  13. while(!queue.isEmpty()) {
  14. depth++;
  15. for(int size = queue.size(); size > 0; size--) {
  16. TreeNode node = queue.poll();
  17. if(node.left != null) {
  18. if(checkNode(node, node.left, x, y, depth, tempList))
  19. return true;
  20. queue.offer(node.left);
  21. }
  22. if(node.right != null) {
  23. if(checkNode(node, node.right, x, y, depth, tempList))
  24. return true;
  25. queue.offer(node.right);
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31. private boolean checkNode(TreeNode parent, TreeNode child, int x, int y, int depth, List<Integer> list) {
  32. if(child.val == x || child.val == y) {
  33. list.add(parent == null? -1 : parent.val);
  34. list.add(depth);
  35. }
  36. if(list.size() == 4) {
  37. //different parent and same depth
  38. if(list.get(0) != list.get(2) && list.get(1) == list.get(3))
  39. return true;
  40. }
  41. return false;
  42. }
  43. //方法二:DFS
  44. public boolean isCousins2(TreeNode root, int x, int y) {
  45. return dfs(null, root, x, y, 0, new ArrayList<>());
  46. }
  47. private boolean dfs(TreeNode parent, TreeNode child, int x, int y, int depth, List<Integer> list) {
  48. if(child == null) return false;
  49. if(child.val == x || child.val == y) {
  50. list.add(parent == null ? -1 : parent.val);
  51. list.add(depth);
  52. }
  53. if(list.size() == 4) {
  54. //if different parent and same depth is true, return true
  55. if(list.get(0) != list.get(2) && list.get(1) == list.get(3))
  56. return true;
  57. }
  58. return dfs(child, child.left, x, y, depth + 1, list) || //
  59. dfs(child, child.right, x, y, depth + 1, list);
  60. }
  61. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. public class CousinsInBinaryTreeTest {
  5. @Test
  6. public void test() {
  7. CousinsInBinaryTree obj = new CousinsInBinaryTree();
  8. assertFalse(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, 4), 4, 3));
  9. assertTrue(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, null, 4, null, 5), 5, 4));
  10. assertFalse(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, null, 4), 2, 3));
  11. }
  12. @Test
  13. public void test2() {
  14. CousinsInBinaryTree obj = new CousinsInBinaryTree();
  15. assertFalse(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, 4), 4, 3));
  16. assertTrue(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, null, 4, null, 5), 5, 4));
  17. assertFalse(obj.isCousins(BinaryTree.integers2BinaryTree(1, 2, 3, null, 4), 2, 3));
  18. }
  19. }

发表评论

表情:
评论列表 (有 0 条评论,125人围观)

还没有评论,来说两句吧...

相关阅读