Press "Enter" to skip to content

Posts published in “Tree”

花花酱 LeetCode 617. Merge Two Binary Trees

Problem

Given two binary trees and 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 them 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 new tree.

Example 1:

Input: 
	Tree 1                     Tree 2                  
          1                         2                             
         / \                       / \                            
        3   2                     1   3                        
       /                           \   \                      
      5                             4   7                  
Output: 
Merged tree:
	     3
	    / \
	   4   5
	  / \   \ 
	 5   4   7

Note:Ā The merging process must start from the root nodes of both trees.


Solution: Recursion

Reuse t1/t2

Create a copy

 

花花酱 LeetCode 655. Print Binary Tree

Problem

Print a binary tree in an m*n 2D string array following these rules:

  1. The row numberĀ mĀ should be equal to the height of the given binary tree.
  2. The column numberĀ nĀ should always be an odd number.
  3. The root node’s value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don’t need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don’t need to leave space for both of them.
  4. Each unused space should contain an empty stringĀ "".
  5. Print the subtrees following the same rules.

Example 1:

Input:
     1
    /
   2
Output:
[["", "1", ""],
 ["2", "", ""]]

Example 2:

Input:
     1
    / \
   2   3
    \
     4
Output:
[["", "", "", "1", "", "", ""],
 ["", "2", "", "", "", "3", ""],
 ["", "", "4", "", "", "", ""]]

Example 3:

Input:
      1
     / \
    2   5
   / 
  3 
 / 
4 
Output:

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

Note:Ā The height of binary tree is in the range of [1, 10].

Solution: Recursion

Compute the layers h of the tree.

shape of the output matrix: h * w (w = 2^h – 1)

pre-order to fill the output matrix

first layer’s root: y1 = 0, x1 = (l1 + r1) / 2 = (0 + w – 1) / 2 (center)

first layer’s left child (2nd layer): y2 = 1, x2 = (l2 + r2) = (l1 + (x1 – 1)) / 2 (center of the left half)

first layer’s right child(2nd layer): y1 = 2, x2 = (l2′ + r2′) = ((x1+1) + r1) / 2 (center of the right half)

Time complexity: O(m*n)

Space complexity: O(m*n)

C++

Python3

花花酱 LeetCode 404. Sum of Left Leaves

Solution: Recursion

Time complexity: O(n)

Space complexity: O(h)

C++

Iterative

 

花花酱 LeetCode 101. Symmetric Tree

Problem

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary treeĀ [1,2,2,3,4,4,3]Ā is symmetric:

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

But the followingĀ [1,2,2,null,3,null,3]Ā is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

Solution: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

Python

Related Problems

花花酱 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