Prerequisite

  • Preorder(전위순회) : Root- Left- Right
  • In-Order(중위순회 ) : Left - Root - Right
  • Postorder(후위순회) : Left - Right - Root

Question 1. Merge two tree node

  • 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.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if(t1 == null && t2 == null) {
            return null;
        }
        int val = (t1 == null ? 0 : t1.val) + (t2 == null ? 0: t2.val);
        TreeNode newNode = new TreeNode(val);

        newNode.left = mergeTrees((t1 == null ? null: t1.left), (t2 == null ? null: t2.left));
        newNode.right = mergeTrees((t1 == null ? null: t1.right), (t2 == null ? null: t2.right));

        return newNode;

    }
}

Question 2. Print tree left to right at same level

static void levelOrder(Node root){
    Queue<Node> queue = new LinkedList<Node>();
    queue.add(root);

    while(!queue.isEmpty()){
        Node current = queue.poll();
        System.out.print(current.data + " ");

        if(current.left != null) queue.add(current.left);
        if(current.right != null) queue.add(current.right);
    }
}

Question 3. Top view of tree

void topView(Node root) {
    Stack<Node> leftStack = new Stack<Node>();
    Queue<Node> rightQueue = new LinkedList<Node>();

    Node left = root.left;
    while(left!=null){
        leftStack.push(left);
        left = left.left;
    }

    Node right = root.right;
    while(right!=null){
        rightQueue.add(right);
        right = right.right;
    }

    while(!leftStack.isEmpty()){
        System.out.print(leftStack.pop().data + " ");
    }
    System.out.print(root.data + " " );
    while(!rightQueue.isEmpty()){
        System.out.print(rightQueue.poll().data + " " );
    }
}

Question 4. Insert into tree

static Node Insert(Node root,int value) {
    if(root == null){
        root = new Node();
        root.data = value;
        return root;
    }
    if(value < root.data){
        root.left = Insert(root.left, value);
    }else{
        root.right = Insert(root.right, value);
    }
    return root;
}

Question 5. Huffman tree

void decode(String S ,Node root)
{
    StringBuffer buf = new StringBuffer("");
    Node cur = root;
    for(int i = 0; i<S.length();i++){
        cur = S.charAt(i) == '0' ? cur.left : cur.right;
        if(cur.left == null && cur.right == null){
            buf.append(cur.data);
            cur = root;
        }
    }
    System.out.println(buf.toString());
}

Question 6. LCA

static Node lca(Node root,int v1,int v2){
    if(root == null){
        return null;
    }

    Node ans = root;
    if(v1 > ans.data && v2 > ans.data){
        ans= lca(root.right, v1, v2);
    }else if(v1 < ans.data && v2< ans.data){
        ans= lca(root.left, v1,v2);
    }
    return ans;
}

results matching ""

    No results matching ""