Part of the exercise questions for the binary tree

Same tree

Link: The same tree
Insert picture description here
Idea: First judge whether the root nodes are the same, if they are different, then false, and then judge the left subtree and the right subtree

class Solution {
    
    
    public boolean isSameTree(TreeNode p, TreeNode q) {
    
    
        if (p == null && q == null) {
    
    
            return true;
        }
        if (p == null || q == null) {
    
    
            return false;
        }
        if (p.val!=q.val){
    
    
            return false;
        }

        if (!isSameTree(p.left, q.left)) {
    
    
            return false;
        }
        return isSameTree(p.right, q.right);
    }
}

Subtree of another tree

Link: Subtree of another tree
Insert picture description here
Idea: Determine whether the tree is the same from the root node, and then the left subtree, the right subtree in turn

class Solution {
    
    
    public boolean isSubtree(TreeNode s, TreeNode t) {
    
    
        if(s==null){
    
    
            return false;
        }
        if (isSameTree(s, t)) {
    
    
            return true;
        }
        if (isSubtree(s.left, t)) {
    
    
            return true;
        }
        return isSubtree(s.right, t);
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
    
    
        if (p == null && q == null) {
    
    
            return true;
        }
        if (p == null || q == null) {
    
    
            return false;
        }
        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

Maximum depth of binary tree

Link: Add link description
Insert picture description here
Idea: Find the height of the binary tree, find the left subtree and the right subtree, then return to the higher height, and then add the root node.

class Solution {
    
    
    public int maxDepth(TreeNode root) {
    
    
        if (root == null) {
    
    
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);

        return Integer.max(left, right) + 1;
    }
}

Balanced binary tree

Link: Balanced Binary Tree
Insert picture description here
Idea: Get the height of the left subtree and the height of the right subtree of each node of the binary tree, and judge whether the absolute value of the difference meets the requirements

class Solution {
    
    
    public boolean isBalanced(TreeNode root) {
    
    
        if (root == null) {
    
    
            return true;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        int diff = Math.abs(left - right);
        if (diff > 1) {
    
    
            return false;
        }
        if (!isBalanced(root.left)) {
    
    
            return false;
        }
        return isBalanced(root.right);

    }

    private int getHeight(TreeNode root) {
    
    
        if (root == null) {
    
    
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        return Integer.max(left, right) + 1;
    }
}

Symmetric Binary Tree

Link: Symmetric Binary Tree
Insert picture description here
Idea: Determine whether the left child and right child of the left subtree are equal to the right child and left child of the right subtree, so that it can be judged whether it is symmetric.

class Solution {
    
    
    public boolean isSymmetric(TreeNode root) {
    
    
        if (root == null) {
    
    
            return true;
        }
        return isSame(root.left, root.right);
    }

    public boolean isSame(TreeNode p, TreeNode q) {
    
    
        if (p == null && q == null) {
    
    
            return true;
        }
        if (p == null || q == null) {
    
    
            return false;
        }
        if (p.val != q.val) {
    
    
            return false;
        }
        if (!isSame(p.left, q.right)) {
    
    
            return false;
        }
        return isSame(p.right, q.left);
    }
}

Nearest common ancestor of binary tree

Link: The nearest common ancestor of the binary tree.
Insert picture description here
Idea: Determine the position of two nodes. If one is left and the other is right, then it is the root node. If it is under the same subtree, then recursively continue to search.

class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        if (root == p || root == q) {
    
    
            return root;
        }
        boolean pindex = contains(root.left, p);
        boolean qindex = contains(root.left, q);
        if (pindex && !qindex) {
    
    
            //p在左子树,q不在
            return root;
        }
        if (qindex && !pindex) {
    
    
            //q在左子树,p不在
            return root;
        }
        if (pindex) {
    
    
            //p、q在左子树,
            return lowestCommonAncestor(root.left, p, q);
        } else {
    
    
            return lowestCommonAncestor(root.right, p, q);
        }
    }

    public boolean contains(TreeNode root, TreeNode node) {
    
    
        if (root == null) {
    
    
            return false;
        }
        if (root == node) {
    
    
            return true;
        }
        if (contains(root.left, node)) {
    
    
            return true;
        }
        return contains(root.right, node);
    }
}

Binary tree creation string

Link: Binary tree creation string
Insert picture description here

class Solution {
    
    
    public String tree2str(TreeNode t) {
    
    
        StringBuilder sb = new StringBuilder();
        doTree2str(t, sb);
        return sb.toString();
    }
    
    private void doTree2str(TreeNode t, StringBuilder sb) {
    
    
        if (t != null) {
    
    
            sb.append(t.val);
            if (t.left != null || t.right != null) {
    
    
                sb.append('(');
                doTree2str(t.left, sb);
                sb.append(')');
                if (t.right != null) {
    
    
                    sb.append('(');
                    doTree2str(t.right, sb);
                    sb.append(')');
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_52142731/article/details/115322790