【LeetCode】Identical Tree, Flip Binary Tree, Symmetrical Binary Tree

100. The same tree

The condition for the two trees to be the same is that the value val of the root node and their left and right subtrees is the same, and the structure is the same, that is, they are exactly the same. Then this question still needs to traverse the two trees at the same time, and it has to be traversed. If we are in In the process of traversal, by setting some conditions that do not meet the same tree, as long as it traverses to a different tree, it will return false. And after each recursion of the left and right subtrees is completed, the judgment is made (each judged once), if it is not satisfied, it will not traverse down, otherwise, it will be traversed all the time.

Different conditions: the left root is empty and the right root is not empty, or, the left root is not empty and the right root is empty, or, the values ​​val of the left and right roots are different.

 algorithm code

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q==null) return true;  //说明本层遍历到末尾了,且本层是相同的树
        if(p==null&&q!=null || p!=null&&q==null || p.val != q.val) return false; //树不相同的全部条件
   
        boolean left = isSameTree(p.left,q.left);
        if(left == false) return false;  //返回fslse说明不相同,结束遍历,返回true则得继续遍历
        boolean right = isSameTree(p.right,q.right);
        if(right == false) return false;  //返回fslse说明不相同,结束遍历,返回true则得继续遍历
        return true;  //能执行到这里说明本层根节点及子树是相同的,再往上一层递归的归
    }
}

 

226. Flip a Binary Tree

That is, start from the root node, flip the left and right subtrees of the root node, then use the left child node as the current root node, flip the left and right subtrees of the left child, and then use the right child node as the current root node, flip the left and right subtrees of the right child. ......recursively downwards in turn, that is, each node is regarded as the current root node, and then its left and right subtrees are flipped, so that the recursion is formed. (you can write a swap function)

 algorithm code

class Solution {
    /*
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root; 
        exchange(root);  //交换函数
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public static void exchange(TreeNode root){ 
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
    }
    */

    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root; 
        TreeNode tmp = root.left; //临时节点,用来交换
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left); //对根的左子树进行交换
        invertTree(root.right); //对根的右子树进行交换
        return root;
    }

}

 

572. A subtree of another tree

The subtree of another tree actually has to be judged that the subtree of the root is different from the subRoot. You can refer to the code of the above two same trees as a function to judge the difference. When traversing, the root tree It needs to be traversed continuously, and the subRoot tree only traverses down when the root tree is the same as its root node (referring to the SubRoot tree). That is, when the function passes a node, it always passes the root node of the subRoot tree. This is an important point.

 algorithm code

class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        //第一种
        /*
        if(isSameTree(root,subRoot)==true)  return true; //  归的条件
        if(root==null ) return false;                    //  归的条件
        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);  //不断的递
        */
        
        //第二种
        /*
        if(root==null ) return false;
        if(isSameTree(root,subRoot)==true)  return true;
        if(isSubtree(root.left,subRoot)==true)  return true;
        if(isSubtree(root.right,subRoot)==true)  return true;
        return false;
        */
    }

    public static boolean isSameTree(TreeNode p, TreeNode q) {  //借用相同的树的代码
        if(p==null&&q==null) return true;
        if(p==null&&q!=null || p!=null&&q==null || p.val != q.val) return false;
   
        boolean left = isSameTree(p.left,q.left);
        if(left == false) return false;
        boolean right = isSameTree(p.right,q.right);
        if(right == false) return false;
        return true;
    }
}

 

Guess you like

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