Bahii honii 3ana problem esmo “Cousins in Binary Tree” 
→ bahii enti bsh ykon y3andik el “root” mt3 a binary Tree okii , with unique values okii 

→ bahii w bsh ykon 3andik two values “x” and “y” , retrun True 

Ken the values mt3 el nodes “x” and “y” are cousins , 

Q- chnowa m3nha “cousins” okii ? 

Zouz nodes of a binary Tree are “cousins” if they have the same depth “in the same level” but in the different parents okii 

Q- bahii ma3louma el depth mt3 el “root” howa =  0 and depth mt3 parent fe depth k howa bedo 
K+1 
Q- chnwowa el the “input” & “output” format honii ? 

—> input : root:TreeNode , x:int , y:int 
—> output : bool 
Q- donc chnowa “lezmana” n3mloo honii ? 

→ awl 7aje lzem ntl3o el level “depth” mt3 kolll 
Node of value “x” and node of value “y” okii 
→ zeda b3ed m9nhom lzem ntl3o el parent node mt3hom 

Ken for example 9nhom fard “level” ame 3andhom nfess el parent node donc return False 
Else True okii 




Tnjm tt3eda be example honii 

Awl w7ed tswer 3andik root=[1,2,3,4] , x=4 ou y=3 okii 

—> donc the node of value “x” —> is at level 2 
—> donc the node of the value “y” —> is at level 1 

Be7kem enou “x” and “y” mch far level direct nrj3 “False” okii 


Q- fema exmple ? 




Bahii 3ana el root = [1,2,3,null,4,null,5] okii , x = 5  , y = 4 okii 

Bahii okii awl 7aje n7wlo ntl3o el level mt3 node of values “x” and value”y” oki 

—> honii el level bsh ykon : mt3 x=5 → 3 
—> honi el level bsh ykon : mt3 y=4 → 3 

Twa awl condition is verfied now we need to check if the two nodes arrive from the same parent okii 
I mean if they arrive from the same parrent just return False & True 
Q- twa b3ed mt3dina fe logic mt3 exmple at hight level can you generate more explicte solution 
Q- even pseudoCode : 

Bsh nrkzo 3le el party mt3 “Traverse + search” okii 
El Traversal bsh yukon a customized iterative dfs okii 
Where in each time we get the desired node we just return it level and it parent node reference okii 

Q- bhii dfs 
—> to help us find the the parent we will check the present the node value from the parent side 
—> we levrage this defintion okii children of each depth k node are at the depth k + 1.
—> which mean : each time we will the check the popped parent node ‘left “ and “righ” node if existe withe value target okii 
—> if present we return a tuple (node(parent node ) , level+1 // the level of the find it node )) 
Stack = [(root,0)] // push the root node and the it’s level oki == 0 
While stack: 
	// pop the root and level ok 
	Node , level = stack.pop() 
	If node.left: 
		If node.left.val==target:
			Return (node,level+1) 
	If node.right:
		If node.right.val == target:
			Return (node,level+1)
	// now just push the reaminding node
	If node.right:
		stack.append(node.right,level+1)
	If node.left:
		stack.append(node.left,level+1) 
Return 


Q- now i want to be my pair programming partner and give me feedback about my approach so 
Far okii 
Q- without given me the full solution 
Q-  just help me building the right mental model for this problem oki



Bahii honii 3ana problem esmo “Cousins in Binary Tree” 
→ bahii enti bsh ykon y3andik el “root” mt3 a binary Tree okii , with unique values okii 

→ bahii w bsh ykon 3andik two values “x” and “y” , retrun True 

Ken the values mt3 el nodes “x” and “y” are cousins , 

Q- chnowa m3nha “cousins” okii ? 

Zouz nodes of a binary Tree are “cousins” if they have the same depth “in the same level” but in the different parents okii 

Q- bahii ma3louma el depth mt3 el “root” howa =  0 and depth mt3 parent fe depth k howa bedo 
K+1 
Q- chnwowa el the “input” & “output” format honii ? 

—> input : root:TreeNode , x:int , y:int 
—> output : bool 
Q- donc chnowa “lezmana” n3mloo honii ? 

→ awl 7aje lzem ntl3o el level “depth” mt3 kolll 
Node of value “x” and node of value “y” okii 
→ zeda b3ed m9nhom lzem ntl3o el parent node mt3hom 

Ken for example 9nhom fard “level” ame 3andhom nfess el parent node donc return False 
Else True okii 




Tnjm tt3eda be example honii 

Awl w7ed tswer 3andik root=[1,2,3,4] , x=4 ou y=3 okii 

—> donc the node of value “x” —> is at level 2 
—> donc the node of the value “y” —> is at level 1 

Be7kem enou “x” and “y” mch far level direct nrj3 “False” okii 


Q- fema exmple ? 




Bahii 3ana el root = [1,2,3,null,4,null,5] okii , x = 5  , y = 4 okii 

Bahii okii awl 7aje n7wlo ntl3o el level mt3 node of values “x” and value”y” oki 

—> honii el level bsh ykon : mt3 x=5 → 3 
—> honi el level bsh ykon : mt3 y=4 → 3 

Twa awl condition is verfied now we need to check if the two nodes arrive from the same parent okii 
I mean if they arrive from the same parrent just return False & True 
Q- twa b3ed mt3dina fe logic mt3 exmple at hight level can you generate more explicte solution 
Q- even pseudoCode : 

Bsh nrkzo 3le el party mt3 “Traverse + search” okii 
El Traversal bsh yukon a customized iterative dfs okii 
Where in each time we get the desired node we just return it level and it parent node reference okii 

Q- bhii dfs 
—> to help us find the the parent we will check the present the node value from the parent side 
—> we levrage this defintion okii children of each depth k node are at the depth k + 1.
—> which mean : each time we will the check the popped parent node ‘left “ and “righ” node if existe withe value target okii 
—> if present we return a tuple (node(parent node ) , level+1 // the level of the find it node )) 
Stack = [(root,0)] // push the root node and the it’s level oki == 0 
While stack: 
	// pop the root and level ok 
	Node , level = stack.pop() 
	If node.left: 
		If node.left.val==target:
			Return (node,level+1) 
	If node.right:
		If node.right.val == target:
			Return (node,level+1)
	// now just push the reaminding node
	If node.right:
		stack.append(node.right,level+1)
	If node.left:
		stack.append(node.left,level+1) 
Return 


Q- now i want to be my pair programming partner and give me feedback about my approach so 
Far okii 
Q- without given me the full solution 
Q-  just help me building the right mental model for this problem oki


Q- now without modifying any think just get an example from the example i choice early one 
Q- and just try to create a manual execution about it oki