Simple Java 2 traversal.


  • 0
    C
        Integer sum;
        boolean res = false;
        public boolean checkEqualTree(TreeNode root) {
            
            sum = getSum(root);
            getSum(root);
            
            return res;
        }
        
        public int getSum(TreeNode node){
            
            if (node == null){
                return 0;
            }
            
            int leftNode = getSum(node.left);
            int rightNode = getSum(node.right);
            
            int value  = node.val + leftNode + rightNode;
            
            if (sum != null){
                
                if ((sum - leftNode == leftNode) && node.left != null){
                    res = true;
                }
                
                if ((sum - rightNode == rightNode) && node.right != null){
                    res = true;
                }
                
            }
            
            return value;
            
        }
    

Log in to reply
 

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