• LeetCode 993. Cousins in Binary Tree


    原题链接在这里: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:

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

    Example 2:

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

    Example 3:

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

    Note:

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

    题解:

    Perform level order tranersal.

    When polling the current node, if its left and right child are not null and they equal to x and y, then x and y are sibling, not cousins, return false.

    Have two boolean value mark if x is found and y is found. If bouth found on the same level, return true.

    Time Complexity: O(n).

    Space: O(n).

    AC Java:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public boolean isCousins(TreeNode root, int x, int y) {
    12         if(root == null){
    13             return false;
    14         }
    15         
    16         LinkedList<TreeNode> que = new LinkedList<>();
    17         que.add(root);
    18         
    19         while(!que.isEmpty()){
    20             int size = que.size();
    21             boolean xFound = false;
    22             boolean yFound = false;
    23             
    24             while(size-- > 0){
    25                 TreeNode cur = que.poll();
    26                 if(cur.val == x){
    27                     xFound = true;
    28                 }
    29                 
    30                 if(cur.val == y){
    31                     yFound = true;
    32                 }
    33                 
    34                 if(cur.left != null && cur.right != null){
    35                     if(cur.left.val == x && cur.right.val == y){
    36                         return false;
    37                     }
    38                     
    39                     if(cur.left.val == y && cur.right.val == x){
    40                         return false;
    41                     }
    42                 }
    43                 
    44                 if(cur.left != null){
    45                     que.add(cur.left);
    46                 }
    47                 
    48                 if(cur.right != null){
    49                     que.add(cur.right);
    50                 }
    51             }
    52             
    53             if(xFound && yFound){
    54                 return true;
    55             }
    56         }
    57         
    58         return false;
    59     }
    60 }
  • 相关阅读:
    串匹配(C/C++实现)
    稀疏数组-矩阵存储【C语言实现】
    mysql frm、MYD、MYI数据文件恢复,导入MySQL中
    我们为什么要分库分表?
    golang 使用goto进行多错误处理
    mongodb 查看、创建、修改、删除索引
    MyBatis中模糊搜索使用like匹配带%字符时失效问题
    MySQL 用 limit 为什么会影响性能?
    【java框架】SpringBoot(10) -- SpringBoot巧用 @Async提升API接口并发能力
    【Java代码之美】 -- Java17新特性初探
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12325305.html
Copyright © 2020-2023  润新知