Brush the number of days just full moon 😊, happy Mid-Autumn Festival! I wish nuggets friends round and round, happy every day!

Ps: Thanks to the nuggets big mooncake gift box

58. Length of the last word

Reverse traversal and set the flag

var lengthOfLastWord = function(s) {
    let count = 0, flag = 0
    for (let i = s.length - 1; i >= 0; i--) {
        if(s[i] ! =' ') {
            flag = 1
            count++
        }
        if (s[i] == ' ' && flag == 1) {return count
            break}}return count
};
Copy the code

102. Sequence traversal of binary trees

var levelOrder = function(root) {
    let res = [], queue = []
    if(! root)return res
    queue.push(root)     // Queue first in, first out

    while (queue.length) {
        let len = queue.length, curLevel = []   // Record the nodes of each layer

        for (let i = 0; i < len; i++) {
            let node = queue.shift()
            curLevel.push(node.val)
            if (node.left) queue.push(node.left)
            if (node.right) queue.push(node.right)
        }

        res.push(curLevel)
    }

    return res
}
Copy the code