[Swift Weekly Contest 124]LeetCode993. 二叉树的堂兄弟节点 | Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and y are cousins.

Example 1:

Input: root = [1,2,3,4], x = 4, y = 3
Output: false

Example 2:

Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true

Example 3:

Input: root = [1,2,3,null,4], x = 2, y = 3
Output: false

Note:

  1. The number of nodes in the tree will be between 2 and 100.
  2. Each node has a unique integer value from 1 to 100.


在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。

如果二叉树的两个节点深度相同,但父节点不同,则它们是一对堂兄弟节点

我们给出了具有唯一值的二叉树的根节点 root,以及树中两个不同节点的值 x 和 y

只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true。否则,返回 false

示例 1:

输入:root = [1,2,3,4], x = 4, y = 3
输出:false

示例 2:

输入:root = [1,2,3,null,4,null,5], x = 5, y = 4
输出:true

示例 3:

输入:root = [1,2,3,null,4], x = 2, y = 3
输出:false 

提示:

  1. 二叉树的节点数介于 2 到 100 之间。
  2. 每个节点的值都是唯一的、范围为 1 到 100 的整数。

Runtime: 12 ms

Memory Usage: 18.4 MB

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var ddd:Int = 0
16     func isCousins(_ root: TreeNode?, _ x: Int, _ y: Int) -> Bool {
17         var px:TreeNode? = dfs(root, nil, x, 0, 1)
18         var py:TreeNode? = dfs(root, nil, y, 0, -1)
19         return x != y && px != nil && py != nil && px!.val != py!.val && ddd == 0
20     }
21
22     func dfs(_ cur:TreeNode?,_ par:TreeNode?,_ x:Int,_ dep:Int,_ mul:Int) -> TreeNode?
23     {
24         if cur == nil {return nil}
25         if cur!.val == x
26         {
27             ddd += dep * mul
28             return par
29         }
30         var res:TreeNode? = dfs(cur!.left, cur, x, dep + 1, mul)
31         if res != nil {return res}
32         res = dfs(cur!.right, cur, x, dep+1, mul)
33         return res
34     }
35 }

原文地址:https://www.cnblogs.com/strengthen/p/10390643.html

时间: 2024-09-30 20:40:11

[Swift Weekly Contest 124]LeetCode993. 二叉树的堂兄弟节点 | Cousins in Binary Tree的相关文章

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <

[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false Note: The number of nodes i

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has

LeetCode练习-求二叉树的深度-Maximu Depth of Binary Tree

题目大意: 很简单,只需要找出一颗二叉树的最大深度即可,貌似没有时间和空间的要求. 求解方法: 更简单,只需要按照宽度优先的方法去查找即可,在这里我用a队列保存待扩展的节点,用b来保存a扩展出来的节点,再利用t中间变量来交换a和b,直到a列队为空时,结束. 注意边界条件,root=NULL时,应该返回0. #include <iostream> #include <queue> using namespace std; //Definition for a binary tree

二叉树的各种遍历算法-leetcode Binary Tree Postorder Traversal 扩展

二叉树的各种遍历方法有  前序遍历   中序遍历    后序遍历  层序遍历.其中前三种遍历有递归程序可以实现,但是我们也有必要掌握其非递归版本的算法实现.正好在leetcode中遇到了遍历二叉树的问题,今天在这里一并总结了. 首先,引用leetcode中关于二叉树节点的定义. 1 // Definition for binary tree 2 struct TreeNode { 3 int val; 4 TreeNode *left; 5 TreeNode *right; 6 TreeNode

LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 后序遍历的顺序是左孩子->右孩子->父节点,对于每个遍历到的节点,执行如下操作: 首先对该节点不断沿左孩子方向向下遍历并入栈,直到左孩子为空 取出栈顶节点,此时该节点为树的最左下节点,若其右孩子不为空,则回到步骤1:若为空说明其为叶子节点,将其值输出到结果中,并记录pre为当前节点 在步骤2判断右孩子是

LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)

题目描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / 2 5 / \ 3 4 6 将其展开为: 1 2 3 4 5 6 解题思路 二叉树转化为链表的基本思想是:对于左孩子转化为右孩子:对于右孩子,拼接到根结点左子树最后一个节点作为右孩子.所以在自上而下转化时,对于每个节点要先保存其右孩子,然后记录转为链表后本子树的最后一个节点并返回给上一个根节点. 代码 1 /** 2 * Definition for a binary tree node. 3 * struct TreeN