Press "Enter" to skip to content

Posts tagged as “subtree”

花花酱 LeetCode 865. Smallest Subtree with all the Deepest Nodes

Problem

Given a binary tree rooted at root, the depth of each node is the shortest distance to the root.

A node is deepest if it has the largest depth possible among any node in the entire tree.

The subtree of a node is that node, plus the set of all descendants of that node.

Return the node with the largest depth such that it contains all the deepest nodes in it’s subtree.

 

Example 1:

Input: [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation:



We return the node with value 2, colored in yellow in the diagram.
The nodes colored in blue are the deepest nodes of the tree.
The input "[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]" is a serialization of the given tree.
The output "[2, 7, 4]" is a serialization of the subtree rooted at the node with value 2.
Both the input and output have TreeNode type.

 

Note:

  • The number of nodes in the tree will be between 1 and 500.
  • The values of each node are unique.

 


Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

v2

Python3

 

花花酱 LeetCode 572. Subtree of Another Tree

Problem

题目大意:判断一棵树是不是另外一棵树的子树。

https://leetcode.com/problems/subtree-of-another-tree/description/

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.

Example 1:
Given tree s:

Given tree t:

Return true, because t has the same structure and node values with a subtree of s.

Example 2:
Given tree s:

Given tree t:

Return false.

Solution: Recursion

Time complexity: O(max(n, m))

Space complexity: O(max(n, m))

C++

Related Problems

 

花花酱 LeetCode 508. Most Frequent Subtree Sum

 

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.

Examples 1
Input:

return [2, -3, 4], since all the values happen only once, return all of them in any order.

Examples 2
Input:

return [2], since 2 happens twice, however -5 only occur once.

Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.

 

Python

 

花花酱 LeetCode 652. Find Duplicate Subtrees

652. Find Duplicate SubtreesMedium730151FavoriteShare

Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.

Two trees are duplicate if they have the same structure with same node values.

Example 1:

The following are two duplicate subtrees:

        1
       / \
      2   3
     /   / \
    4   2   4
       /
      4

2
/
4

and

4

Therefore, you need to return above trees’ root in the form of a list.

Solution 1: Serialization

Time complexity: O(n^2)
Space complexity: O(n^2)

C++

Solution 2: int id for each unique subtree

Time complexity: O(n)
Space complexity: O(n)

C++