Cousins in Binary Tree Solutions in C++
Number 993
Difficulty Easy
Acceptance 52.0%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/cousins-in-binary-tree/// Author : Hao Chen// Date : 2019-04-30/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/class Solution {public:bool isCousins(TreeNode* root, int x, int y) {int dx=0, dy=0;TreeNode *px=root, *py=root;dx = DepthAndParent(root, px, 0, x);dy = DepthAndParent(root, py, 0, y);if (dx && dy){return (dx == dy && px != py);}return false;}int DepthAndParent(TreeNode* root, TreeNode*& parent, int depth, int x) {if (!root) return 0;if ( root->val == x) return depth;int d=0;parent = root;if ( ( d = DepthAndParent(root->left, parent, depth+1, x)) > 0 ) return d;parent = root;if ( ( d = DepthAndParent(root->right, parent, depth+1, x)) > 0 ) return d;return 0;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/cousins-in-binary-tree//// Author : liuyubobobo/// Time : 2019-02-16#include <iostream>#include <cassert>using namespace std;/// Two-Pass DFS/// Time Complexity: O(n)/// Space Complexity: O(h)/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool isCousins(TreeNode* root, int x, int y) {if(!root || root->val == x || root->val == y)return false;pair<int, int> p1; // depth, parentif(!dfs(root, x, 0, p1)) return false;pair<int, int> p2;if(!dfs(root, y, 0, p2)) return false;return p1.first == p2.first && p1.second != p2.second;}private:bool dfs(TreeNode* node, int x, int d, pair<int, int>& p){if(node->left && node->left->val == x){p = make_pair(d + 1, node->val);return true;}if(node->right && node->right->val == x){p = make_pair(d + 1, node->val);return true;}if(node->left && dfs(node->left, x, d + 1, p))return true;if(node->right && dfs(node->right, x, d + 1, p))return true;return false;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/cousins-in-binary-tree//// Author : liuyubobobo/// Time : 2019-02-18#include <iostream>#include <cassert>#include <unordered_map>using namespace std;/// One-Pass DFS/// Using HashMap to record the result :-)////// Time Complexity: O(n)/// Space Complexity: O(n)/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:bool isCousins(TreeNode* root, int x, int y) {if(!root || root->val == x || root->val == y)return false;unordered_map<int, pair<int, int>> record; // depth, parentdfs(root, 0, record);return record[x].first == record[y].first && record[x].second != record[y].second;}private:void dfs(TreeNode* node, int d, unordered_map<int, pair<int, int>>& record){if(node->left) {record[node->left->val] = make_pair(d + 1, node->val);dfs(node->left, d + 1, record);}if(node->right){record[node->right->val] = make_pair(d + 1, node->val);dfs(node->right, d + 1, record);}}};int main() {return 0;}