当前位置:网站首页>[binary tree] 993 Cousins in Binary Tree
[binary tree] 993 Cousins in Binary Tree
2022-06-23 03:41:00 【Twilight_ years】
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.
The question : Define cousin node : Same number of layers , But the parent nodes are different .
Ideas :dfs, Record the number of layers and the value of the parent node .
Returns an array , It contains the number of layers and the value of the parent node
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;
}
}
边栏推荐
- [OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
- 数据加密技术之源代码加密
- JS Part 4
- Tcapulusdb Jun · industry news collection (III)
- How to print multiple barcode labels on one sheet of paper
- Even if you don't learn gradle, these common development operations are worth mastering
- 线上MySQL的自增id用尽怎么办?
- What about the high cost of storage system? The original computer room can save so much money!
- How to batch generate jan13 barcode
- Tcapulusdb Jun · industry news collection (V)
猜你喜欢

1058 multiple choice questions (20 points)

innodb_ruby 视角下 MySQL 记录增删改

聊聊内存模型和内存序

【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行

【贪心】leetcode991. Broken Calculator

第一批00后下场求职:不要误读他们的“不一样”

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

mysql常用指令
![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]

Insert sort directly
随机推荐
Heavyweight review: strategies for reliable fMRI measurements
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]
Three ways to export excel from pages
【LeetCode】179. Maximum number
How to print array contents
Goframe framework: log configuration management
【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
Flink practice tutorial: advanced 7- basic operation and maintenance
【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
[advanced Android] entrusted by kotlin
Know res.send() and res.end() of Express
innodb_ruby 视角下 MySQL 记录增删改
[OWT] OWT client native P2P E2E test vs2017 build 2: test unit construction and operation
An implementation of universal interface caching Middleware
【LeetCode】23. Merge K ascending linked lists
【LeetCode】23. 合并K个升序链表
What is the difference between ArrayList and LinkedList?
Composition and simple classification of IP addresses
How to batch generate jan13 barcode
Downloading sqlserver versions (2016-2019)