[LeetCode] Symmetric Binary Tree Balanced Binary Tree

 Symmetric Binary Tree

That is, first judge that the left and right subtrees of the root node are different. If they are the same, then judge the left subtree of the left child and compare the right subtree of the right child. It is only symmetrical when they are all the same)..... recursively in turn, and set some conditions that do not meet the same conditions during the process.

 algorithm code

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true; //由于此题至少有一个根节点,可写可不写
        return isSymmetricChild(root.left,root.right);
    }

    public static boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree){
        if(leftTree==null && rightTree==null) return true;  //两个都为空
        if(leftTree!=null && rightTree==null || leftTree==null && rightTree!=null) return false; //一个为空一个不为空
        if(leftTree.val != rightTree.val) return false;  //值不相同
       
        /*
        boolean left = isduicheng(leftTree.left,rightTree.right);
        boolean right = isduicheng(leftTree.right,rightTree.left);
        return left&&right;
        */

        return isSymmetricChild(leftTree.left,rightTree.right) && isSymmetricChild(leftTree.right,rightTree.left);//和上面注释的作用一样

    }
}

 

 balanced binary tree

The definition of a balanced binary tree: the absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

The essence of this question is to find the height of the tree. As long as you can write the function code to find the height of the tree, it is basically no problem, because when we find the height of the tree, we continuously return to the current state from the bottom to the top . The maximum value of the height of the left and right subtrees of the subtree + 1 , and finally get the height of the root node, that is, the height of the tree. Then we can make some changes to the function of calculating the height of the number. When the height of the left and right subtrees of the current subroot node is obtained each time, the absolute value is calculated for the difference. If the difference is less than or equal to 1, it means that the current subroot is Balanced, then traverse. If the difference is greater than 1, it means that the current subroot is unbalanced, so there is no need to traverse down, which can save a certain amount of time consumption.

 

 

algorithm code

class Solution {
    public boolean isBalanced(TreeNode root) {
        if(getHeight(root)==-1) return false;
        return true;
    }

    public static int getHeight(TreeNode root){  //求树的高度的函数
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        if(leftHeight<0 || rightHeight<0) return -1; 
        if(Math.abs(leftHeight-rightHeight)>1) return -1; //说明树不平衡了

        return leftHeight>rightHeight?leftHeight+1:rightHeight+1; //返回当前根的最大深度即高度

    }
}

Guess you like

Origin blog.csdn.net/m0_73381672/article/details/132099382