java algorithm binary tree

Keywords: less

1. Find the maximum or minimum depth of binary tree

    public int maxDepth(Tree root){
        if(root == null)
            return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

    public int minDepth(Tree root){
        if(root == null)
            return 0;
        return 1 + Math.min(maxDepth(root.left), maxDepth(root.right));
    }

2. Invert a binary tree

    public Tree invertTree(Tree root){
        if(root == null){
            return null;
        }
        Tree left = invertTree(root.left);
        Tree right = invertTree(root.right);
        root.right = left;
        root.left = right;
        return root;
    }

3. Compare whether two binary trees are the same

    public boolean compareTree(Tree root01,Tree root02){
        if(root01 == null && root02 == null){
            return true;
        }else if(root01 == null || root02 == null){
            return false;
        }

        boolean left = compareTree(root01.left,root02.left);
        if(!left){
            return false;
        }
        boolean right = compareTree(root01.right,root02.right);
        if(!right){
            return false;
        }
        return true;
    }

4. Calculate the total number of nodes in a binary tree

    public int allTreeNode(Tree root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return allTreeNode(root.left) + allTreeNode(root.right);
    }

5. Calculate whether there is a path from the child node to the root node of the tree so that their value is sum

    public boolean pathTree(Tree root,int sum){
        if(root == null){
            return false;
        }
        if(root.right == null && root.left == null){
            return sum == root.val;
        }

        sum -= root.val;
        if(sum < 0){
            return false;
        }
        return pathTree(root.right,sum) || pathTree(root.left,sum);
    }

6. Find the sum of all the left nodes of a binary tree

    public int leftSum(Tree root){
        if(root == null || (root.left == null && root.right == null)){
            return 0;
        }

        int leftVal = 0;
        if(root.left != null){
            leftVal = root.left.val;
        }
        return leftVal + leftSum(root.left) + leftSum(root.right);
    }

 

7. Give a binary tree and return all the combined strings from the child node to the root node

    public List<StringBuilder> binaryTree(Tree root){
        List<StringBuilder> returnStr = new LinkedList<>();
        if(root == null){
            return returnStr;
        }
        if(root.right ==null && root.left == null){
            returnStr.add(new StringBuilder(root.val + ""));
            return returnStr;
        }
        returnStr.addAll(binaryTree(root.left));
        returnStr.addAll(binaryTree(root.right));

        //Add current number
        for(StringBuilder str : returnStr){
            str.insert(0,root.val + "->");
        }
        return returnStr;
    }

 

8. Give a binary tree and a number sum, and find the sum of all the paths from root to leaf. The starting point is not necessarily at the root node. You can start from a child node, but you can only go down

    public int pathSum3(Tree root,int sum){
        if(root == null){
            return 0;
        }
        return findPath(root,sum) + pathSum3(root.left,sum) + pathSum3(root.right,sum);
    }

    private int findPath(Tree node, int num){
        if(node == null){
            return 0;
        }
        if(node.right == null && node.left == null){
            if(node.val == num){
                return 1;
            }
            return 0;
        }
        num -= node.val;
        if(num < 0){
            return 0;
        }
        return findPath(node.left,num) + findPath(node.right,num);
    }

9. Give a binary search number (the value of each node is greater than the value of its left node and less than the value of its right node) Find the nearest common ancestor point for any two nodes

    public Tree binarySearchTree(Tree tree,Tree note1,Tree note2){
        if(tree == null){
            return null;
        }
        if(note1.val < tree.val && note2.val < tree.val){
            return binarySearchTree(tree.left,note1,note2);
        }
        if(note1.val > tree.val && note2.val > tree.val){
            return binarySearchTree(tree.right,note1,note2);
        }
        return tree;
    }

10. Verify that the tree is a binary search tree

    public boolean isBinarySearchTree(Tree tree){
        if(tree == null){
            return true;
        }
        if(tree.left != null && tree.left.val >= tree.val){
            return false;
        }
        if(tree.right != null && tree.right.val <= tree.val){
            return false;
        }
        return isBinarySearchTree(tree.left) && isBinarySearchTree(tree.right);
    }

11. Convert the ordered array into a balanced binary search tree (the absolute value of the difference between the height of left and right nodes shall not exceed 1)

    public Tree paseBinarySearchTree(int[] array){
        int min = array[0];
        int max = array[array.length - 1];
        return binarySearchTree(array,min,max);
    }

    private Tree binarySearchTree(int[] array,int min,int max){
        if(min <= max){
            int mid  = (min + max) / 2;
            Tree left = binarySearchTree(array,min,mid -1);
            Tree right = binarySearchTree(array,mid + 1,max);
            Tree tree = new Tree(array[min]);
            tree.left = left;
            tree.right = right;
            return tree;
        }
        return null;
    }

12. Find the tree with the smallest num in the binary search tree. Left center right search

    private int num;
    private Tree treeNode;

    public Tree getBinarySearchTree(Tree tree,int k){
        runBinarySearchTree(tree,k);
        return treeNode;
    }


    private void runBinarySearchTree(Tree tree,int k){
        if(tree == null || treeNode != null){
            return ;
        }
        getBinarySearchTree(tree.left,k);
        num ++;
        if(num == k){
            treeNode = tree;
        }
        getBinarySearchTree(tree.right,k);
    }

13. Find the nearest common ancestor of two points in a binary tree

    private boolean note01;
    private boolean note02;
    private Tree commonTree;

    public void commonAncestor(Tree tree,Tree one,Tree two){
        if(tree == null){
            return ;
        }
        if(note01 && note02){
            return;
        }
        commonAncestor(tree.left,one,two);
        if(!note01 && tree == one){
            note01 = true;
        }
        if(!note02 && tree == two){
            note02 = true;
        }
        commonAncestor(tree.right,one,two);
        commonTree = tree;
    }

 

Posted by Kairu on Tue, 26 Nov 2019 10:30:35 -0800