*/publicbooleanisCousins(TreeNode root,intx,inty){int[] arr = getTreeDepth(root, x);int[] arr2 = getTreeDepth(root, y);if(arr.length <1|| arr2.length <1) {returnfalse; }returnarr[0] != arr2[0] && arr[1] == arr2[1]; }publicint[] getTreeDepth(TreeNode root,intnum) ...
In a binary tree, the root node is at depth0, and children of each depthknode are at depthk+1. Two nodes of a binary tree arecousinsif they have the same depth, but have different parents. We are given therootof a binary tree with unique values, and the valuesxandyof two different...
* Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} ...
0104-Maximum-Depth-of-Binary-Tree 0105-Construct-Binary-Tree-from-Preorder-and-Inorder Traversal 0106-Construct-Binary-Tree-from-Inorder-and-Postorder Traversal 0107-Binary-Tree-Level-Order-Traversal-II 0108-Convert-Sorted-Array-to-Binary-Search-Tree 0109-Convert-Sorted-List...
My Solutions to Leetcode problems. All solutions support C++ language, some support Java and Python. Multiple solutions will be given by most problems. Enjoy:) 我的Leetcode解答。所有的问题都支持C++语言,一部分问题支持Java语言。近乎所有问题都会提供多个算
在二叉树中,根节点位于深度0处,每个深度为k的节点的子节点位于深度k+1处。 如果二叉树的两个节点深度相同,但父节点不同,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点root,以及树中两个不同节点的值x和y。 只有与值x和y对应的节点是堂兄弟节点时,才返回true。否则,返回false。
Two nodes of a binary tree arecousinsif they have the same depth, but have different parents. We are given therootof a binary tree with unique values, and the valuesxandyof two different nodes in the tree. Returntrueif and only if the nodes corresponding to the valuesxandyare cousins. ...
1 class Solution 2 { 3 public: 4 bool judge(TreeNode* root, int x, int y) 5 { 6 if(root!=NULL) 7 { 8 9 if(root->left&&root->right...
We are given therootof a binary tree with unique values, and the valuesxandyof two different nodes in the tree. Returntrueif and only if the nodes corresponding to the valuesxandyare cousins. 题目分析及思路 定义二叉树的深度:根结点的深度为0,深度为k的结点的孩子结点的深度为k+1。如果二叉树里...
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. Example 1: ...