Wednesday, January 6, 2021

Leetcode discuss: 1305. All Elements in Two Binary Search Trees

 Here is the link. 

Jan. 6, 2020
It is my mock interview perform. My goal is to take some risk. It should not have TLE concern. What I did is to apply inorder traversal for two trees separately, and then call C# List.AddRange to merge two lists, and then call Sort API.

It is a good practice. It should take me less than 15 minutes.

 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
public class Solution {
    public IList<int> GetAllElements(TreeNode root1, TreeNode root2) {
        var list = new List<int>();         
        traverseInOrder(root1, list);
        var list2 = new List<int>(); 
        traverseInOrder(root2, list2);
        return list; 
    private void traverseInOrder(TreeNode root1, IList<int> list)
        if(root1 == null)
        traverseInOrder(root1.left, list); 
        traverseInOrder(root1.right, list);         

No comments:

Post a Comment