From 8390c9fd269b4f2bced85ed8f103c40e4d962d9b Mon Sep 17 00:00:00 2001 From: Ahsan Adeeb <116491229+adeebahsan1@users.noreply.github.com> Date: Sun, 24 Dec 2023 21:48:34 +0530 Subject: [PATCH] Update CHECK COUSINS.txt --- TEST 3/CHECK COUSINS.txt | 155 ++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 90 deletions(-) diff --git a/TEST 3/CHECK COUSINS.txt b/TEST 3/CHECK COUSINS.txt index 559b65f..8f06ad8 100644 --- a/TEST 3/CHECK COUSINS.txt +++ b/TEST 3/CHECK COUSINS.txt @@ -1,20 +1,20 @@ -import java.util.ArrayList; - - -public class solution { - /* Binary Tree Node class - * - * class BinaryTreeNode { - T data; - BinaryTreeNode left; - BinaryTreeNode right; +import java.util.ArrayList; - public BinaryTreeNode(T data) { - this.data = data; - } - } - */ +public class Solution { + + /* Binary Tree Node class + * + * class BinaryTreeNode { + * T data; + * BinaryTreeNode left; + * BinaryTreeNode right; + * + * public BinaryTreeNode(T data) { + * this.data = data; + * } + * } + */ /* Time Complexity - O(N) @@ -22,81 +22,56 @@ public class solution { where N is the number of nodes in the tree and H is the height of the tree -*/ - - - - public static boolean areNodesSibling(BinaryTreeNode root, int a, int b) - { - if (root == null) - { - return false; - } - - if (root.left != null && root.right != null) - { - if (root.left.data == a && root.right.data == b) - { - return true; - } - if (root.left.data == b && root.right.data == a) - { - return true; - } - } - - if (areNodesSibling(root.left, a, b)) - { - return true; - } - if (areNodesSibling(root.right, a, b)) - { - return true; - } - - return false; - } - - public static int findLevel(BinaryTreeNode root, int x, int level) - { - if (root == null) - { - return 0; - } - if (root.data == x) - { - return level; - } - - // if x is found in left subtree - int lev = findLevel(root.left, x, level + 1); - if (lev != 0) - { - return lev; - } - - return findLevel(root.right, x, level + 1); - } - - public static boolean isCousin(BinaryTreeNode root, int p, int q) - { - + */ + + public static boolean areNodesSibling(BinaryTreeNode root, int a, int b) { + if (root == null) { + return false; + } + + if (root.left != null && root.right != null) { + if (root.left.data == a && root.right.data == b) { + return true; + } + if (root.left.data == b && root.right.data == a) { + return true; + } + } + + if (areNodesSibling(root.left, a, b)) { + return true; + } + if (areNodesSibling(root.right, a, b)) { + return true; + } + + return false; + } + + public static int findLevel(BinaryTreeNode root, int x, int level) { + if (root == null) { + return 0; + } + if (root.data == x) { + return level; + } + + // if x is found in the left subtree + int lev = findLevel(root.left, x, level + 1); + if (lev != 0) { + return lev; + } + + return findLevel(root.right, x, level + 1); + } + + public static boolean isCousin(BinaryTreeNode root, int p, int q) { // a and b are the given two nodes - if (p != q && findLevel(root, p, 1) == findLevel(root, q, 1) && !areNodesSibling(root, p, q)) - { - return true; - } - else - { - return false; - } - } + if (p != q && findLevel(root, p, 1) == findLevel(root, q, 1) && !areNodesSibling(root, p, q)) { + return true; + } else { + return false; + } + } } - - -// public static boolean isCousin(BinaryTreeNode root, int p, int q) { -// // Write your code here - -// } - pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy