Java simple implemented code


  • 0
    S

    '''

     public class Solution {
    public List<TreeNode> list  =  new ArrayList<>();
    
    public int sum(TreeNode root){
        if(root==null) return 0;
        else return root.val+ sum(root.left)+sum(root.right);
    }
    public int value (TreeNode root){
        if(root==null)return 0;
        if(root.left==null && root.right==null) return 0;
        if(root.left==null) return sum(root.right);
        if(root.right==null) return sum(root.left);
        return Math.abs(sum(root.left) - sum(root.right));
    }
    public void in(TreeNode root){
        if(root==null) return;
        list.add(root);
        in(root.left);
        in(root.right);
    }    
    
    
    public int findTilt(TreeNode root) {
        int a=0;
        in(root);
        for(int i=0;i<list.size();i++){
            int b = Math.abs(value(list.get(i)));
            System.out.println(b);
            a+=b;
        }
        return a;
    }
    }
    

    '''


Log in to reply
 

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