You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a list of TreeNodes (see the definition below). Find out if all these nodes belong to the same valid binary tree.
Example 1:
Let's say we have the following binary tree
1
↙ ↘
2 3
↙
4
We can create it like this
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
n1.left = n2;
n1.right = n3;
n3.left = n4;
Input: [n4, n2, n3, n1]
Output: true
1
↙ ↘
2 3
↙
4
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
n1.left = n2;
n1.right = n3;
n3.left = n4;
Input: [n2, n3, n1]
Output: false
Explanation: l4 is a part of the tree but it's missing in the input list so return false.
TreeNode class:
class TreeNode:
def __init__(self, val = 0, left = None, right: None):
self.val = val
self.left = left
self.right = None
Given a list of
TreeNodes
(see the definition below). Find out if all these nodes belong to the same valid binary tree.Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
TreeNode
class:The function:
For more details
The text was updated successfully, but these errors were encountered: