当前位置:网站首页>【二叉树】993. Cousins in Binary Tree
【二叉树】993. Cousins in Binary Tree
2022-06-23 03:32:00 【暮色_年华】
Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise.
Two nodes of a binary tree are cousins if they have the same depth with different parents.
Note that in a binary tree, the root node is at the depth 0, and children of each depth k node are at the depth k + 1.
题意:定义堂兄弟节点:层数相同,但是父节点不相同。
思路:dfs,记录层数和父节点的值。
返回一个数组,里面存层数和父节点的值
class Solution {
public boolean isCousins(TreeNode root, int x, int y) {
int[] a = dfs(root,x,0,-1);
int[] b = dfs(root,y,0,-1);
if(a[0] == b[0]){
return a[1] != b[1];
}
return false;
}
int[] dfs(TreeNode root,int x,int level,int p){
if(root == null){
return new int[]{};
}
if(root.val == x){
return new int[]{level,p};
}
int[]a = dfs(root.left,x,level+1,root.val);
int[]b = dfs(root.right,x,level+1,root.val);
if(a.length == 0) return b;
return a;
}
}
边栏推荐
- Golang resource embedding scheme
- Quickly grab the red envelope cover of Tencent blue whale New Year! Slow hands!
- To implement a task scheduling system, it is enough to read this article
- Salesforce heroku (V) application in salesforce (canvasapp)
- Pre and post processing of pytest
- Integrate Tencent maps with micro build and low code
- How to install redis version 5.0.8 on the pagoda panel
- Composition and simple classification of IP addresses
- Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
- JS how to delete an item specified in an array
猜你喜欢

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e

Encryption related to returnee of national market supervision public service platform

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

Gakataka student end to bundle Version (made by likewendy)
![Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]](/img/4b/dd272f98b89a157180bf68570d2763.jpg)
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
![[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]](/img/f1/972a760459a6d599b5681aa634df09.jpg)
[quick view] Analysis on the development status and future development trend of the global and Chinese diamond cultivation industry in 2021 [figure]
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

Jmeter- (V) simulated user concurrent login for interface test

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
![Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]](/img/28/ebfc25ec288627706e15a07e6bdb77.jpg)
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
随机推荐
Build a weather forecast applet using a widget
JS judge the mobile terminal and PC terminal
Build information query applet by using micro build
I Arouter framework analysis
Email authentication bypass
Downloading sqlserver versions (2016-2019)
DAAS architecture and Implementation (I)
Micro build low code to realize user login and registration
[Alibaba middleware technology series] "Nacos technology" service registration and discovery related principle analysis
Methods for MySQL to avoid inserting duplicate records
On the way home from the Spring Festival transportation, traffic visualization will escort you
Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
Chapter IV open source projects and deployment
What are the advantages of the completely free and open source flutter? How to learn about flutter?
C. Differential Sorting
How to batch print serial and repeated barcode data
Copy system disk
Flink practice tutorial: advanced 7- basic operation and maintenance
Troubleshooting and solution of error 400 in easygbs video platform
Storage resources revitalize the system and redefine "hyper fusion"