03-26 Binary Tree Level Order Traversal(BFS로), 71. Simplify Path

Binary Tree Level Order Traversal

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function (root) {
  //큐를 꺼내기 전에 그요소에 들어있는 자식들을 큐에 추가해줌
  let queue = [];
  let answer = [];
  queue.push(root);
  while (queue.length) {
    let layer = [];

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

71. Simplify Path

Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.

The canonical path should have the following format:

  • The path starts with a single slash '/'.
  • Any two directories are separated by a single slash '/'.
  • The path does not end with a trailing '/'.
  • The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')

Return the simplified canonical path.

Example 1:

Input: path = "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

Example 2:

Input: path = "/../"
Output: "/"
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.

Example 3:

Input: path = "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

Example 4:

Input: path = "/a/./b/../../c/"
Output: "/c"

Constraints:

1 <= path.length <= 3000
path consists of English letters, digits, period '.', slash '/' or '_'.
path is a valid absolute Unix path.

/**
 * @param {string} path
 * @return {string}
 */
var simplifyPath = function(path) {
    let splitPath = path.split('/');
    let stack = [];
    for(let i = 0; i < splitPath.length; i++){
        if(splitPath[i] === '.' || splitPath[i] === ''){
            continue;
        }
        if(splitPath[i] === '..'){
            stack.pop();
            continue;
        }
        stack.push(splitPath[i]);
    }
    return '/' + stack.join('/');
};

103. Binary Tree Zigzag Level Order Traversal

풀이

var zigzagLevelOrder = function(root) {
   if(!root) return [];
   let queue = [];
   let answer = [];
   let flag = true;
   queue.push(root);
   while(queue.length){
       let layer = [];
       let len = queue.length;
       for(let i = 0; i < len; i++){
           let temp = queue.shift();
           
           
           if(temp){
               if(flag){
                   layer.push(temp.val);
               }else{
                   layer.unshift(temp.val);
               }
               
           }
           if(temp && temp.left){
               queue.push(temp.left);
           }
           if(temp && temp.right){
               queue.push(temp.right);
           }
       }
       answer.push(layer);
       flag = !flag
   }
   return answer;
};

199. Binary Tree Right Side View

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var rightSideView = function(root) {
    if(!root) return [];
    let queue = [];
    let answer = [];
    queue.push(root);
    while(queue.length){
        let len = queue.length;
        for(let i = 0; i < len; i++){
            let temp = queue.shift();
            if(temp.left){
                queue.push(temp.left);
            }
            if(temp.right){
                queue.push(temp.right);
            }
            if(i === len-1){
                answer.push(temp.val);
            }
        }
        
    }
    return answer;
};

소수점 반올림등 소수점 표시할 때 :
ex)let num = 0.1666666666666 -> 소수점 6자리에서 반올림 Math.round(num * 10**6)/10**6 = 0.166667 또는 num.toFixed(6) = "0.166667"(문자열로됨)
참조 : 아는만큼 보여요 블로그

세상에는 정말 뛰어난 사람들이 많다. 자만하지말고 주께 의지하고 꾸준히 나아가자.

좋은 웹페이지 즐겨찾기