kymr / daily-study

2 stars 0 forks source link

Week2 Coding - How are all leaves of a binary search tree printed? #17

Closed kymr closed 6 years ago

kymr commented 6 years ago

Problem

kymr commented 6 years ago

Basic Template

kymr commented 6 years ago

Analysis Requirements

kymr commented 6 years ago

Solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void printLeafValues(TreeNode root) {

        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<Treenode>();
        stack.push(root);

        while (stack.empty() == false) {
            TreeNode current = stack.pop();

            boolean isLeaf = current.left == null && current.right == null;

            if (isLeaf) {
                System.out.println(current.val);    
            }

            if (current.left != null) {
                stack.push(current.left)
            }

            if (current.right != null) {
                stack.push(current.right);
            }
        }
    }
}
kymr commented 6 years ago

Explanation

kymr commented 6 years ago

Question

kymr commented 6 years ago

Solution for Order

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void printLeafValues(TreeNode root) {

        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<Treenode>();
        stack.push(root);

        while (stack.empty() == false) {
            TreeNode current = stack.pop();

            boolean isLeaf = current.left == null && current.right == null;

            if (isLeaf) {
                System.out.println(current.val);    
            }

            if (current.right != null) {
                stack.push(current.right);
            }

            if (current.left != null) {
                stack.push(current.left)
            }
        }
    }
}
kymr commented 6 years ago

Explanation for Order

kymr commented 6 years ago

Question

kymr commented 6 years ago

Solution with in-order traversal

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void printLeafValues(TreeNode root) {
        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<TreeNode>();
        Set<TreeNode> addedSet = new HashSet<TreeNode>();
        stack.push(root);

        while (stack.empty() == false) {
            TreeNode current = stack.peek();

            boolean isLeftComplete = current.left == null || addedSet.contains(current.left);

            if (isLeftComplete) {
                stack.pop();
                addedSet.add(current);

                if (current.right != null) {
                    stack.push(current.right);
                } else {
            if (current.left == null) {
                System.out.println(current.val);
            }
        {
            } else {
                stack.push(current.left);
            }
        }
    }
}
kymr commented 6 years ago

Explanation for In-order traversal

kymr commented 6 years ago

There is very simple solution for this

public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) { val = x; }
 }

public class Solution {
    public ArrayList<Integer> inorderTraversal(TreeNode root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
         ArrayList<Integer> lst = new ArrayList<Integer>();

        if(root == null)
            return lst; 

        Stack<TreeNode> stack = new Stack<TreeNode>();
        //define a pointer to track nodes
        TreeNode p = root;

        while(!stack.empty() || p != null){

            // if it is not null, push to stack
            //and go down the tree to left
            if(p != null){
                stack.push(p);
                p = p.left;

            // if no left child
            // pop stack, process the node
            // then let p point to the right
            }else{
                TreeNode t = stack.pop();
                lst.add(t.val);
                p = t.right;
            }
        }

        return lst;
    }
}