Recursive solution of JAVA


  • 0
    J
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root==null){
                return true;
            }
            return subfunction(root.left, root.right);
        }
        private boolean subfunction(TreeNode leftNode, TreeNode rightNode){
            if(leftNode==null && rightNode==null){
                return true;
            }
            else if(leftNode!=null&&rightNode!=null){
                if (leftNode.val==rightNode.val){
                    return subfunction(leftNode.left,rightNode.right)&&subfunction(leftNode.right,rightNode.left);
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
            
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.