Dfs java solution


  • 0
    L
    public class Solution {
    private int sum = 0;
    public int sumNumbers(TreeNode root) {
        dfs(root,0);
        return sum;
    }
    private void dfs(TreeNode root,int pathSum){
        if(root == null) return;
        if(root.left == null && root.right == null){
            sum += 10*pathSum + root.val;
            return ;
        }
        dfs(root.left,10*pathSum + root.val);
        dfs(root.right,10*pathSum + root.val);
    }
    

    }


Log in to reply
 

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