当前位置:网站首页>[leetcode sword finger offer 28. Symmetric binary tree (simple)]

[leetcode sword finger offer 28. Symmetric binary tree (simple)]

2022-04-23 21:20:00 Minaldo7

subject :

 Insert picture description here
 Insert picture description here

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

The problem solving process :

Copy a root node , Then compare the left branch with the right branch .

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

        return dfs(root, root);
    }

    public boolean dfs(TreeNode root1, TreeNode root2){
    
        if(root1 == null && root2 == null) return true;
        if(root1 == null || root2 == null) return false;
        if(root1.val == root2.val)
            return dfs(root1.left,root2.right) && dfs(root1.right,root2.left);
        return false;
    }
}

Execution results :

 Insert picture description here

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479611.html