Knowee
Questions
Features
Study Tools

Given the roots of two binary trees p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1:Input: p = [1,2,3], q = [1,2,3]Output: trueExample 2:Input: p = [1,2], q = [1,null,2]Output: falseExample 3:Input: p = [1,2,1], q = [1,1,2]Output: false Constraints:The number of nodes in both trees is in the range [0, 100].-104 <= Node.val <= 104

Question

Given the roots of two binary trees p and q, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. Example 1:Input: p = [1,2,3], q = [1,2,3]Output: trueExample 2:Input: p = [1,2], q = [1,null,2]Output: falseExample 3:Input: p = [1,2,1], q = [1,1,2]Output: false Constraints:The number of nodes in both trees is in the range [0, 100].-104 <= Node.val <= 104

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution for the problem:

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def isSameTree(p, q):
    if p and q:
        return p.val == q.val and isSameTree(p.left, q.left) and isSameTree(p.right, q.right)
    return p is q

This function

Similar Questions

If two trees have same structure and but different node content, then they are called ___a.Synonyms treesb.Joint treesc.Equivalent treesd.Similar trees

You are given two binary trees root1 and root2.Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.Return the merged tree.Note: The merging process must start from the root nodes of both trees.

Is Balanced TreeGiven an array of unique elements, construct a Binary Search Tree and check if its balanced. A tree is said to be balanced if for every node, the difference between the height of its child nodes is not greater than 1.Input FormatFirst line of each test case contains N - number of nodes in the BST. The next line contains N unique integers - value of the nodes.Constraints1 <= N <= 10000 <= ar[i] <= 10000Output FormatFor each test case, print "Yes" if the Binary Search Tree is balanced, "No" otherwise, separated by newline.Sample input6 50 30 70 20 40 60Sample outputYes

Given the root of a binary tree, invert the tree, and return its root. Example 1:Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1]

Consider the below codetakeInput() print("Enter root data") rootData = int(input()) if (rootData == -1) return None root = BinaryTreeNode(rootData) root.left = takeInput() root.right = takeInput() return rootWhat will be the input(excluding -1) to above code to construct this tree ? 1) 2 7 2 6 5 11 5 9 4 2) 2 7 5 6 11 2 5 4 9 3) 2 7 5 2 6 9 5 11 4

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.