Level traverse method


  • 0
    H

    """

    public int sumNumbers(TreeNode root) {
        if (root == null)
            return 0;
        Queue<TreeNode> que = new LinkedList<TreeNode>();
        que.offer(root);
        int sum = 0;
        while (!que.isEmpty()){
            TreeNode cur = que.poll();
            if (cur.left == null && cur.right == null){
                sum += cur.val;
            }
            if (cur.left != null){
                cur.left.val = cur.left.val + 10 * cur.val;
                que.offer(cur.left);
            }
            if (cur.right != null){
                cur.right.val = cur.right.val + 10 * cur.val;
                que.offer(cur.right);
            }
        }
        return sum;
    }
    

    """


Log in to reply
 

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