Java solution occurred COMPILE ERROR!


  • 0
    T

    Why there is COMPILE ERROR?!?!?
    The system shows : Line 22: error: incompatible types: TreeNode cannot be converted to Solution.TreeNode

    I have no idea. It runs ok on my own computer :(

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
    	public class TreeNode {
    		int val;
    		TreeNode left;
    		TreeNode right;
    		TreeNode(int x) { val = x; }
    
    		public int getVal() {
    			return val;
    		}
    
    		public boolean isLeafNode() {
    			return null == left && null == right;
    		}
    
    		public void setLeft(TreeNode node) {
    			this.left = node;
    		}
    
    		public TreeNode getLeft() {
    			return left;
    		}
    
    		public void setRight(TreeNode node) {
    			this.right = node;
    		}
    
    		public TreeNode getRight() {
    			return right;
    		}
    	}
    
    	public static void main(String[] args) {
    		Solution solution = new Solution();
    		TreeNode root = solution.new TreeNode(4);
    
    		TreeNode node2 = solution.new TreeNode(2);
    		TreeNode node7 = solution.new TreeNode(7);
    		TreeNode node1 = solution.new TreeNode(1);
    		TreeNode node3 = solution.new TreeNode(3);
    		TreeNode node6 = solution.new TreeNode(6);
    		TreeNode node9 = solution.new TreeNode(9);
    
    		root.setLeft(node2);
    		root.setRight(node7);
    		node2.setLeft(node1);
    		node2.setRight(node3); 
    		node7.setLeft(node6); 
    		node7.setRight(node9); 
    
    		root = solution.invertTree(root);
    
    		System.out.println("--After:--");
    
    		solution.traverseTree(root);
    
    	}
    
        public TreeNode invertTree(TreeNode root) {
            if (!root.isLeafNode()) {
            	TreeNode leftNode = root.getLeft();
            	TreeNode rightNode = root.getRight();
            	swap(root);
    	        invertTree(leftNode);
    	        invertTree(rightNode);
            }
    
            return root;
        }
    
        public void traverseTree(TreeNode root) {
        	System.out.println(root.getVal());
        	if (root.isLeafNode()) {
            	return ;
            }
    
            traverseTree(root.getLeft());
            traverseTree(root.getRight());
        }
    
    	public static void swap(TreeNode node) {
    		TreeNode tmp = node.left;
    		node.left = node.right;
    		node.right = tmp;
    	}
    }
    

Log in to reply
 

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