题目:993.二叉树的堂兄弟节点

news/2025/3/20 14:52:46/

题目来源:

        leetcode题目,网址:993. 二叉树的堂兄弟节点 - 力扣(LeetCode)

解题思路:

       广度优先遍历二叉树,判断深度及父节点是否相同。

解题代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public boolean isCousins(TreeNode root, int x, int y) {if(root.val==x || root.val==y)      //根节点一定无堂兄弟节点return false;Queue<TreeNode> queue=new LinkedList<>();queue.offer(root.left);queue.offer(root.right);int flagX=-1;int flagY=-1;while(!queue.isEmpty()){int size=queue.size();for(int i=0;i<size;i++){TreeNode thisNode=queue.poll();if(thisNode==null){continue;}if(thisNode.val==x)flagX=i;else  if(thisNode.val==y)flagY=i;else{queue.offer(thisNode.left);queue.offer(thisNode.right);   }}if(flagX!=-1|| flagY!=-1){break;}}if(flagX ==-1|| flagY==-1){return false;}return !(flagX/2==flagY/2);}}
 

总结:

        官方题解将两个都找到了,但广度优先时某些情况下只需找到一个即可。另外我在向队列中添加节点时,节点个数只能为 0 个或 2 个,这样可以利用与 2 之商判断父节点是否相同。



http://www.ppmy.cn/news/822994.html

相关文章

UVA 993 Product of digits

点击打开链接 Product of digits For a given non-negative integer number N , find the minimal naturalQ such that the product of all digits ofQ is equal N . Input The first line of input contains one positive integer number, which is the number of data sets.…

Leetcode 993:二叉树的堂兄弟节点(超详细的解法!!!)

在二叉树中&#xff0c;根节点位于深度 0 处&#xff0c;每个深度为 k 的节点的子节点位于深度 k1 处。 如果二叉树的两个节点深度相同&#xff0c;但父节点不同&#xff0c;则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root&#xff0c;以及树中两个不同…

993

思路只要想通就很简单了&#xff0c;实际上就是把一个数因式分解为若干个2-9的数的成绩&#xff0c;直接枚举找就行 // // Name : 993.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C, Ansi-style …

求13-23+33-43+…+973-983+993-1003的值

求13-2333-43…973-983993-1003的值 public class T6 {public static void main(String[] args) {int a,b,sum;a13;b1;sum0;for(;a<1003;a10) {suma*bsum;bb*(-1);}System.out.println("13-2333-43…973-983993-1003的值为 "sum);} }程序执行结果&#xff1a; 1…

python操作imap 993端口 mail pop3

import time from imap_tools import MailBox, AND import imap_tools import re # Get date, subject and body len of all emails from INBOX folder # server 10.67.49.79 with MailBox(mail server ip或域名).login(ymail.com,password) as mailbox:for msg in mailbox.fet…

uva 993 Product of digits(分解因子)

题目连接&#xff1a;993 - Product of digits 题目大意:给出一个正整数&#xff0c; 要求找到一个自然数&#xff0c;使得该自然数的每一位的数字的乘积等于正整数。并且要求最小&#xff0c;不存在输出-1. 解题思路&#xff1a;将正整数分解因子&#xff0c;注意这里要从9开始…

UVa 993: Product of digits

这道题很简单。先将N用2,3,5,7&#xff08;即10以内的素数&#xff09;分解因数&#xff08;需要先特殊判断N不为1&#xff09;&#xff0c;然后将可以合并的因数合并&#xff08;如2*2合并成4&#xff0c;&#xff09;这样求得的结果位数会减少&#xff0c;大小肯定会小一些。…

UVa 993 - Product of digits

【链接】 http://uva.onlinejudge.org/index.php?optioncom_onlinejudge&Itemid8&category113&pageshow_problem&problem934 【原题】 For a given non-negative integer number N , find the minimal natural Q such that the product of all digits of Q …