In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the cousin node of a binary tree". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the cousin node of a binary tree"!
Cousin node of binary tree
In a binary tree, the root node is at depth 0, and the child nodes of each node with depth k are at depth kroom1.
If the two nodes of the binary tree are of the same depth, but the parent nodes are different, they are a pair of cousins.
We give the root node root of a binary tree with a unique value, and the values x and y of two different nodes in the tree.
True is returned only if the node corresponding to the values x and y is a cousin. Otherwise, return false.
Tip:
The number of nodes in a binary tree is between 2 and 100.
The value of each node is a unique integer ranging from 1 to 100.
Example 1:
Input: root = [1, 2, 3, 4], x = 4, y = 3, output: false/** * 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 {int fatherless color 1; int yearly fatherly color 2; int Xendepe; public boolean isCousins (TreeNode root, int x, int y) {dfs (root,root.left,x,y,1); dfs (root,root.right,x,y,1); return (x_dep==y_dep) & } public void dfs (TreeNode father,TreeNode node,int x node.val int y dep) {if (node = = null) {return;} if (node.val = = x) {x_dep = dep; x_father = father.val;} else if (node.val = = y) {y_dep = dep; y_father = father.val } else {dfs (node,node.left,x,y,dep+1); dfs (node,node.right,x,y,dep+1) Thank you for your reading. the above is the content of "what is the cousin node of a binary tree". After the study of this article, I believe you have a deeper understanding of what is the cousin node of a binary tree. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.