Press "Enter" to skip to content

Posts tagged as “tree”

花花酱 LeetCode 872. Leaf-Similar Trees

Problem

Consider all the leaves of a binary tree.Ā  FromĀ left to right order, the values of thoseĀ leaves form aĀ leaf value sequence.

For example, in the given tree above, the leaf value sequence isĀ (6, 7, 4, 9, 8).

Two binary trees are consideredĀ leaf-similarĀ if their leaf value sequence is the same.

ReturnĀ trueĀ if and only if the two given trees with head nodesĀ root1Ā andĀ root2Ā are leaf-similar.

Note:

  • Both of the given trees will have betweenĀ 1Ā andĀ 100Ā nodes.

Solution: Recursion

Time complexity: O(n1 + n2)

Space complexity: O(n1 + n2)

Python

 

花花酱 LeetCode 501. Find Mode in Binary Search Tree

Problem

Given a binary search tree (BST) with duplicates, find all theĀ mode(s)Ā (the most frequently occurred element) in the given BST.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keysĀ less than or equal toĀ the node’s key.
  • The right subtree of a node contains only nodes with keysĀ greater than or equal toĀ the node’s key.
  • Both the left and right subtrees must also be binary search trees.

 

For example:
Given BSTĀ [1,null,2,2],

returnĀ [2].

Note:Ā If a tree has more than one mode, you can return them in any order.

Follow up:Ā Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

Solution1: Recursion w/ extra space

Time complexity: O(n)

Space complexity: O(n)

 

Solution2: Recursion w/o extra space

Two passes. First pass to find the count of the mode, second pass to collect all the modes.

Time complexity: O(n)

Space complexity: O(1)

 

花花酱 LeetCode 429. N-ary Tree Level Order Traversal

Problem

Given an n-ary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example, given aĀ 3-aryĀ tree:

 

 

We should return its level order traversal:

[
     [1],
     [3,2,4],
     [5,6]
]

Note:

  1. The depth of the tree is at mostĀ 1000.
  2. The total number of nodes is at mostĀ 5000.

Solution1: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

Solution2: Iterative

 

花花酱 LeetCode 559. Maximum Depth of N-ary Tree

Problem

Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

For example, given aĀ 3-aryĀ tree:

 

 

We should return its max depth, which is 3.

Note:

  1. The depth of the tree is at mostĀ 1000.
  2. The total number of nodes is at mostĀ 5000.

Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

Related Problems

花花酱 LeetCode 590. N-ary Tree Postorder Traversal

Problem

Given an n-ary tree, return theĀ postorderĀ traversal of its nodes’ values.

 

For example, given aĀ 3-aryĀ tree:

 

Return its postorder traversal as:Ā [5,6,3,2,4,1].

 

Note:Ā Recursive solution is trivial, could you do it iteratively?

Ā 

Solution 1: Recursive

C++

Solution 2: Iterative

C++

Related Problems