C++怎么实现二叉树的堂兄弟节点查询

其他教程   发布日期:前天 19:07   浏览次数:83

这篇文章主要介绍了C++怎么实现二叉树的堂兄弟节点查询的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C++怎么实现二叉树的堂兄弟节点查询文章都会有所收获,下面我们一起来看看吧。

    一.二叉树的堂兄弟节点

    1.题目描述

    在二叉树中,根节点位于深度

    1. 0
    处,每个深度为
    1. k
    的节点的子节点位于深度
    1. k+1
    处。

    如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。

    我们给出了具有唯一值的二叉树的根节点

    1. root
    ,以及树中两个不同节点的值
    1. x
    1. y

    只有与值

    1. x
    1. y
    对应的节点是堂兄弟节点时,才返回
    1. true
    。否则,返回
    1. false

    力扣:力扣

    2.问题分析

    题目中很详细的给出了判断堂兄弟节点的条件:①两个节点深度相同②父节点不同

    由此我们可以通过BFS和DFS找到题目给定的两个值对应的二叉树结点,记录这两个结点的深度和父节点,最后通过判断堂兄弟结点的条件从而判断是否为堂兄弟结点.

    3.代码实现

    1.BFS解法

    1. // x 的信息
    2. int x;
    3. TreeNode xParent;
    4. int xDepth;
    5. boolean xFound = false;
    6. // y 的信息
    7. int y;
    8. TreeNode yParent;
    9. int yDepth;
    10. boolean yFound = false;
    11. public boolean isCousins(TreeNode root, int x, int y) {
    12. this.x = x;
    13. this.y = y;
    14. LinkedList<TreeNode> queue = new LinkedList<>();
    15. int depth = 0;
    16. if (root != null) {
    17. queue.offer(root);
    18. if (root.val == x || root.val == y) {
    19. return false;
    20. }
    21. }
    22. while (!queue.isEmpty()) {
    23. int size = queue.size();
    24. for (int i = 0; i < size; ++i) {
    25. TreeNode node = queue.poll();
    26. if (node.left != null) {
    27. queue.offer(node.left);
    28. if (node.left.val == x) {
    29. xParent = node;
    30. xDepth = depth;
    31. }
    32. if (node.left.val == y) {
    33. yParent = node;
    34. yDepth = depth;
    35. }
    36. }
    37. if (node.right != null) {
    38. queue.offer(node.right);
    39. if (node.right.val == x) {
    40. xParent = node;
    41. xDepth = depth;
    42. }
    43. if (node.right.val == y) {
    44. yParent = node;
    45. yDepth = depth;
    46. }
    47. }
    48. }
    49. depth++;
    50. }
    51. return xDepth == yDepth && xParent != yParent;
    52. }

    2.DFS解法

    1. // x 的信息
    2. int x;
    3. TreeNode xParent;
    4. int xDepth;
    5. boolean xFound = false;
    6. // y 的信息
    7. int y;
    8. TreeNode yParent;
    9. int yDepth;
    10. boolean yFound = false;
    11. public boolean isCousins(TreeNode root, int x, int y) {
    12. this.x = x;
    13. this.y = y;
    14. dfs(root, 0, null);
    15. return xDepth == yDepth && xParent != yParent;
    16. }
    17. public void dfs(TreeNode node, int depth, TreeNode parent) {
    18. if (node == null) {
    19. return;
    20. }
    21. if (node.val == x) {
    22. xParent = parent;
    23. xDepth = depth;
    24. xFound = true;
    25. } else if (node.val == y) {
    26. yParent = parent;
    27. yDepth = depth;
    28. yFound = true;
    29. }
    30. // 如果两个节点都找到了,就可以提前退出遍历
    31. // 即使不提前退出,对最坏情况下的时间复杂度也不会有影响
    32. if (xFound && yFound) {
    33. return;
    34. }
    35. dfs(node.left, depth + 1, node);
    36. if (xFound && yFound) {
    37. return;
    38. }
    39. dfs(node.right, depth + 1, node);
    40. }

    二.二叉树的堂兄弟节点 II

    1.题目描述

    给你一棵二叉树的根

    1. root
    ,请你将每个节点的值替换成该节点的所有 堂兄弟节点值的和。

    如果两个节点在树中有相同的深度且它们的父节点不同,那么它们互为 堂兄弟。

    请你返回修改值之后,树的根

    1. root

    注意,一个节点的深度指的是从树根节点到这个节点经过的边数。

    力扣:力扣

    2.问题分析

    每一次只需要求出下一层的所有节点的和,然后减去非子结点的值,就是其堂兄弟结点值的和了.

    3.代码实现

    1. public TreeNode replaceValueInTree(TreeNode root) {
    2. root.val = 0;
    3. ArrayList<TreeNode> queue = new ArrayList<>();
    4. queue.add(root);
    5. while (!queue.isEmpty()) {
    6. ArrayList<TreeNode> tmp = queue;
    7. queue = new ArrayList<>();
    8. int nextLevelSum = 0; // 下一层的节点值之和
    9. for (TreeNode node : tmp) {
    10. if (node.left != null) {
    11. queue.add(node.left);
    12. nextLevelSum += node.left.val;
    13. }
    14. if (node.right != null) {
    15. queue.add(node.right);
    16. nextLevelSum += node.right.val;
    17. }
    18. }
    19. // 再次遍历,更新下一层的节点值
    20. for (TreeNode node : tmp) {
    21. int childrenSum = (node.left != null ? node.left.val : 0) +
    22. (node.right != null ? node.right.val : 0);
    23. if (node.left != null)
    24. node.left.val = nextLevelSum - childrenSum;
    25. if (node.right != null)
    26. node.right.val = nextLevelSum - childrenSum;
    27. }
    28. }
    29. return root;
    30. }

    以上就是C++怎么实现二叉树的堂兄弟节点查询的详细内容,更多关于C++怎么实现二叉树的堂兄弟节点查询的资料请关注九品源码其它相关文章!